How do I create 5 buttons and assign individual click events dynamically?
Solution 1
This is what Nick is talking about are your two options (You should be able to run this code and see both options):
public Form1()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
Button button = new Button();
button.Location = new Point(20, 30 * i + 10);
switch (i)
{
case 0:
button.Click += new EventHandler(ButtonClick);
break;
case 1:
button.Click += new EventHandler(ButtonClick2);
break;
//...
}
this.Controls.Add(button);
}
for (int i = 0; i < 5; i++)
{
Button button = new Button();
button.Location = new Point(160, 30 * i + 10);
button.Click += new EventHandler(ButtonClickOneEvent);
button.Tag = i;
this.Controls.Add(button);
}
}
void ButtonClick(object sender, EventArgs e)
{
// First Button Clicked
}
void ButtonClick2(object sender, EventArgs e)
{
// Second Button Clicked
}
void ButtonClickOneEvent(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
// now you know the button that was clicked
switch ((int)button.Tag)
{
case 0:
// First Button Clicked
break;
case 1:
// Second Button Clicked
break;
// ...
}
}
}
Solution 2
I assume you're in a loop and do something like this?
Button newButton = new Button();
newButton.Click += new EventHandler(newButton_Clicked);
You're registering the same method for all buttons. You'll need individual methods for each button. Alternatively, you can assign each button a different identifying property and in your handler, check to see which button was the sender.
From there you can take appropriate action.
Solution 3
Guessing what you might have tried: Yes, all buttons fire their events to the same method, but the sender
-parameter of your callback method contains a reference to the button that actually caused the specific event.
Raj
Software Engineer (.Net, Java), love learning new technology.
Updated on July 05, 2022Comments
-
Raj almost 2 years
I need to create 5 buttons dynamically on windows form and each button should respond to click event. I tried it but all buttons are responding to same event.
-
Raj over 14 yearswill these buttons responds to click events, like when i click button1, message should display "Button 1 Clicked", on clicking button2 it should show "Button 2 Clicked." and so on...
-
Moumit about 8 yearsAnd how it solve the problem?
-
Kiquenet over 5 years
public void AddHandler(Control control, string eventName, Delegate handler) { control.GetType().GetEvent(eventName).AddEventHandler(control, handler); }
-
AlainD about 2 yearsGood answer. Some general advice to newer WinForms developers for creating controls dynamically: (1) On your form in Design view, drop the control you want to create. Size and position the control where you need it. (2) Find its' name (say
button1
) and open the formDesigner.cs
file. (3) Locate the control of interest and copy/paste that code somewhere (e.g. Notepad). (4) Return to the design view and delete your temporary control. (5) Use the code you copy/pasted to write your dynamic creation code as @SwDevMan81 does here.