Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i create a button click event for dynamic button array?
please help me.
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 15:18pm    
Not very typical MSDN walk-through. My 5.
--SA
Wendelius 23-Mar-11 15:36pm    
Good link, +5.
When you create the button in your code add describe the event handler, something like:
Button btn = new Button();
...
btn.Click += new EventHandler(commonEventHandlerMethodForAllButtons);

If you like, you can use Tag property to add information about the button which you can use in the method handling the event. For an example:
C#
enum Action {
    Some = 1,
    Other = 2
}
...
void SomeMethod() {
   Button btn = new Button();
   btn.Tag = Action.Some;
   btn.Click += new EventHandler(common_Click);
   ...
}
 
void common_Click(object sender, EventArgs e) {
   switch (((Action)(Button)sender).Tag) {
      case Action.Some:
         // do something
         break;
      case ...
   }
}
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 15:17pm    
Mika, I would vote 5, if not "Some description". It's only useful when the only action is to output the text "Some description" somewhere. If you simply say the Tag can be used to carry some data used in the delegate, I'll re-vote. Also, anything using non-anonymous form of delegate is bad.
If you chose to improve it, I would vote.
--SA
Wendelius 23-Mar-11 15:36pm    
Good note. I added the tag info later at the end of the post after writing the event handler subscription. That's how it got a bit mixed up. Updated the example to clarify the description for the tag.
Sergey Alexandrovich Kryukov 23-Mar-11 20:57pm    
I have to vote 3 this time, sorry. The tag could be some structure, bool, enum, but not string.
It's a very bad idea to rely on string comparison anywhere, especially to hard-code a string.
Change is, mistype it -- no compilation error to detect the problem. This is one of the most common and disastrous approach to design of functionality.
I'm really sorry.
--SA
Wendelius 24-Mar-11 1:43am    
No need to be sorry, you're correct. Changed the code so that bad solution won't haunt me anymore :)
Sergey Alexandrovich Kryukov 24-Mar-11 2:40am    
Now I vote 4.
By the way did you know:
btn.Click += new EventHandler(commonEventHandlerMethodForAllButtons);
can be just
btn.Click += commonEventHandlerMethodForAllButtons;
common_Click is still breaking Microsoft naming conventions.
(Yes, Microsoft auto-generated code breaks Microsoft naming conventions, but this is even good: a good reason to rename everything which is auto-generated to tell the attended code from unattended.) Also, anything without anonymous delegate makes little sense.
I can explain why. Why carrying (object sender, EventArgs e)? You don't use them always. The anonymous unties that from the call.
--SA

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