I'm making a windows forms application using C#. I add buttons and other controls programmatically at run time. I'd like to know how to handle those buttons' click events?
+13
A:
Try the following
Button b1 = CreateMyButton();
b1.Click += new EventHandler(this.MyButtonHandler);
...
void MyButtonHandler(object sender, EventArgs e) {
...
}
JaredPar
2010-01-20 19:35:14
thank you, but it didn't really fit my needs. I tried searching the web based on what you gave me, but I couldn't find or understand anything.Thing is, I have an array of buttons. And I'd like to know which button is clicked.
jello
2010-01-21 13:19:53
A:
Check out this example How to create 5 buttons and assign individual click events dynamically in C#
SwDevMan81
2010-01-21 01:18:00
A:
seems like this works, while adding a tag with each element of the array
Button button = sender as Button;
do you know of a better way?
jello
2010-01-21 16:11:15