Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to make which is better performance and readable code

When i have 10 buttons each button have procedure to do.

should i use one event for every button click ?
or one event to all buttons click and target Every Button Separately inside event For Example :

Button b = Sender As Button;
if (b.Name = "btn1")
....
if (b.Name = "btn2")
...


Which good approach to follow and good performance, IN Delphi And In C# ?

What I have tried:

Trying Multiple Event-Driven Procedures to single event for all buttons
Posted
Updated 25-Feb-17 0:26am
Comments
[no name] 25-Feb-17 6:18am    
"should i use one event for every button click ? or one event to all buttons click", it depends. The only person that can answer your question is you.

1 solution

Generally speaking, I use a separate event handler method for each separate function, rather than button: if your btn1 opens a file, and btn2 saves a file then I would use separate handler methods. If instead they acted as number buttons on a calculator, I'd use a single method for them all, and use the Tag property of the button to give me the value:
C#
private void NumberButton_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        int value = (int)b.Tag;
        ...
        }
    }
It's about the functionality, not about the code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900