简体   繁体   中英

How to apply multiple events on a single button click

So i am using Windows Forms App on visual studio c#. I am developing a game. For that i have given an "INSTRUCTIONS" Form. In that form i want to give the instructions. The game has 4 modes so for each mode i want to give separate instructions. I have used groupBox for providing instructions for each mode. Which means that in the groupBox i have added textBox for instruction. Now for 4 modes i am using 4 groupBox. Now the question is that how to open groupBox2 then groupBox3 then groupBox4 by clicking on same "NEXT" Button?? Like when the form loads the groupBox1 loads which is for first mode. Now when the user clicks on NEXT button, the groupBox1 gets invisible and the groupBox2 becomes visible which is for second mode. Then again when user clicks on same Next button the groupBox2 gets invisible and groupBox 3 becomes visible. In the same way for "PREVIOUS" button which will make groupBox3 invisible and groupBox2 visible.

There are a couple reasonable approaches to this.

One would be to keep a variable int step (or similar) in the class data, and inside the button's unified click handler do switch (step) .

Another would be to have separate click handlers, and in the first click handler, do NextButton.Click -= Step1NextClicked; NextButton.Click += Step2NextClicked; NextButton.Click -= Step1NextClicked; NextButton.Click += Step2NextClicked; and similarly in the others.

A variation on the second approach would be for the button click handler to not have any logic of its own, but just call an Action which points to a separate handler function at each stage. Then you can update the Action in a single assignment when changing stages, instead of having to remove the old handler and add the new one.

Yet another approach would be to swap not just the groupbox, but a borderless panel holding both the groupbox and the button. Then you actually have multiple Next buttons that display one at a time in the exact same screen location.

Personally I would use the first option, since it's very efficient. If I had more than a handful of different behaviors, I would probably use the Action approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM