I have 22 buttons, and i want all of them to run the same method, which will:
1.Get the name of the button that was hit.
2.Perform a task based on which button was hit.(probably a switch statement).
Is there and easier/ faster way to set the click methods without setting all 22 buttons?
is there at least a way to find the button that was clicked?
Getting a button click without knowing the button
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Kirtan PatelRank #292 · 8.6K reputationPosted May 20, 2009, 12:33 PM EDT
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void addHandlers()
{
button2.Click += new EventHandler(button1_Click);
button3.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("You Clicked " + ((Button)(sender)).Text);
string ButtonText = ((Button)(sender)).Text;
switch (ButtonText)
{
case "button1":
//Do Somthing
case "button2":
//Do Somthing
default:
//Do Somthing
}
}
private void Form1_Load(object sender, EventArgs e)
{
addHandlers();
}
}
}
patrickRank #4,458 · 342 reputationPosted May 21, 2009, 6:05 AM EDT