views:

443

answers:

3

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