Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 20 button on a winform. On each button click I want to execute the same code. I only need to pass a unique value (in my case I use the Tag field) from each button to know which button was clicked (i.e. MessageBox.Show(button.Tag.ToString()). This specific Tag-value must be transferred to the code which I can use for each button-click.
How can I do that easily without writing the same code for each button_click event?
Is there an example available which shows a solution?

What I have tried:

Now I use the same code for each button.
Posted
Updated 15-May-19 4:12am

You can do something like this:
button1.Click += new EventHandler(Button_Click);
button2.Click += new EventHandler(Button_Click);
button3.Click += new EventHandler(Button_Click);

public void Button_Click(object sender, EventArgs e)
{
  Button btn = (Button)sender;
  if (btn.Tag == null) return;
  
  if (btn.Tag.equals("button1"))
  {
    // Do something
  }
}
 
Share this answer
 
v2
Create a single button click event, and connect that to each button. You get the information about the sender in the parameters passed in to the event handler.
 
Share this answer
 
Point the click handler of the button to the same method.
Visual studio helps you create a new handler, but it also allow you to select an existing.

Or you can do it through code if you get lazy. Good programmers are lazy enough to spend 4 hours programming a solution to a stupid repetitive task that would take 10 minutes to do manually :)
 
Share this answer
 
If you have so many buttons and want the handler to rapidly get the number of the button clicked then it may be simpler to create a button array. VB.NET does not provide a language feature for this (like VB6) but it is easy to do it manually. In the form declare
C#
Button  btnarray[];

and bind the array elements to your buttons, in the Form_Load method:
C#
btnarray=new Button[20];
btnarray[0]=Button1;
btnarray[1]=Button2;
...

Then the handler can just search the array for the matching index:
C#
int idx;
for(idx=0; idx<btnarray.Length(); idx++)
 if (sender.Equals(this.btnarray[idx]))  {break;}
 
Share this answer
 
You can assign the same event handler (method) to all your buttons.
The Click event handler is usually defined as
C#
void button_Click(object sender, ClickEventArgs e)
{
   if (sender.Equals(this.button1))
   {
      // button1 was pressed
   }
   else if (sender.Equals(this.button2))
   {
      // button2 was pressed
   }
   // ...
   // Assuming here your buttons are named button1 .. button20.
}

In the designer, just assign the single method to the Click event of all buttons.
 
Share this answer
 
Connect to the single click event as mentioned in the previous solutions, and then try something like the following

C#
void button_Click(object sender, ClickEventArgs e)
{
    MessageBox.Show(((Button)sender).Tag.ToString());
}
 
Share this answer
 
I used following code to solve the problem:
I use a public function which is called from a button_click event.

public void btnclick(int i)
{
//code to execute in which I use variable i because I need to modify data table on
//specific rows. Each row matches with one button, therefor I need the correct number.
}


private void button1_Click(object sender, EventArgs e)
{
btnclick(1);
}

private void button2_Click(object sender, EventArgs e)
{
    btnclick(2);
}

And so on for every button.
 
Share this answer
 
Comments
CHill60 15-May-19 11:20am    
Solution 1 is far and away better though

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