1

it's me again, tried not to bother you guys so soon, but me got a question...ssss... ... ...

Okay, so, when I create application with multiple buttons, I usually handle their click event by assigning the same event handler for every button. I give them their unique names or tags and then with switch-case loop, using their names or tags I assign them their job, duty, mission, void, you know... So, this looks like something like this in my code:

 private void button_Click(object sender, EventArgs e) { Button B = (Button)sender; switch (B.Name) { case "START": optionStart(); break; case "LOAD": optionLoad(); break; case "EXIT1": Application.Exit(); break; case "EXIT2": backToMainMenu(); break; } } 

I am facing here couple of problems: 1. Using my "technique" this whole thing is quite a mess when you reach about 20-30 buttons. 2. Buttons with same name must be handled differently so I name them usually EXIT1, EXIT2, EXIT3 witch makes me lose it after few more exits...

So, anyone has a alternative suggestion or solution or perhaps some guide? I'm a shameless man, I'll accept anything! (just joking... I don't accept American Express)

2
  • 3
    Instead of hooking up each Button.Click event to the same event handler - create an event handler for each button. If your button names are good, that is, they are self documenting - your code will become more readable. Commented Aug 4, 2013 at 13:16
  • It's a good suggestion... I'll try it out :) Commented Aug 4, 2013 at 13:19

3 Answers 3

2

How about using a Dictionary. Register the method you wish to run and then in your handler - just look it up and use it.

An example (no error handling for simplicity) -

Initialize by:

m_clickHandler = new Dictionary<Button, Action>(); m_clickHandler.add(startButton, optionStart); 

Handle by

 private void button_Click(object sender, EventArgs e) { m_clickHandler[sender as Button].Invoke(); } 

Another way is by using Lambda expressions. You can hook the event directly to what you wish to run. This will save you the ugly parameters handlers normally get.

button1.Click += (a,b) => Foo(); 
Sign up to request clarification or add additional context in comments.

3 Comments

I have not been using it so far simply because I wasn't aware of it, thank you for info, I am still reading a programing books and I have not come up to that parts yet :)
Coding is the best and only way to learn how to code :) By the way, you should select the answer that best suits your needs (keyboardP's or mine - and accept it).
I am aware of it, It's just that I think it's a bit too early, maybe I get some more alternatives, who knows, if nothing comes up in a day or two, I'll take my pick, don't worry I don't forget here ;)
1

You'll still have to type out the actions but this might make it easier for you to maintain.

Dictionary<string, Action> actionDict = new Dictionary<string, Action>(); { { "START", optionStart}, { "LOAD", optionLoad}, { "EXIT1", () => { Application.Exit(); }}, { "EXIT2", backToMainMenu } }; private void button_Click(object sender, EventArgs e) { Button B = (Button)sender; if(B != null) { Action methodToCall = null; if (actionDict.TryGetValue(B.Name, out methodToCall)) { methodToCall(); } } } 

You can then add or remove from the dictionary easily without having to modify the switch statement(s).

Alternatively, you could store an Action in the Button's Tag property and invoke that. (Although, as mentioned in the comments, this isn't extensible. You could add a List but that would just get messy and I wouldn't recommend it).

However, overall, why do you have so many exit options? It seems that you might be able to redesign your screen flow to better accommodate so many ways of exiting.

7 Comments

A collection initializer would be quite nice here :-)
I thought about using tags as well, but I think it has a serious problem of extensibility. What'll happen if he would like to handle more events?
Yeah, the Tag option isn't the most ideal for general purposes.
I was answering on your response with rather long comment then your response vanished together with my comment and I am not retyping all that x( In short: thank you, you've helped a lot
What was the gist of it? Sorry about that, I thought there was a bigger problem with the code but it was fine :)
|
1

i have a workaround for you.. you may set

int Type1 = 1; int Type2 = 2; button1.tag = Type1; button2.tag = Type2; button3.tag = Type1; 

on event

Button B = (Button)sender; switch ((int)B.Tag) { case 1: optionStart(); break; case 2: optionLoad(); break; case 3: Application.Exit(); break; case 4: backToMainMenu(); break; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.