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?
Loading
Kirtan PatelPosted May 20, 2009, 4:33 PM
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();
}
}
}
patrickPosted May 21, 2009, 10:05 AM