C# how to handle multiple button click events?

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)


ANSWERS:


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();

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.


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;
        }


 MORE:


 ? C# Add Controls To Panel In a Loop
 ? C# Add Controls To Panel In a Loop
 ? C# Add Controls To Panel In a Loop
 ? dynamically adding buttons and button objects in panel
 ? Populate Buttons With Database Values c#
 ? Adding GUI controls dynamically
 ? trying to add/remove controls to a form
 ? removing dynamically created controls C#
 ? Add columns to a datatable in c#?
 ? ASP NET - C#: Access controls by Id