I have some problems with the understanding of casting buttons, maybe you can help me. If i have for example 3 buttons and i wont have very long code i can cast them, right?
For example:
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
If i give evry button a tag, i sould be able to ask if the button was clicked, right?
private void MachMalClick(object sender, EventArgs e)
{
var myButton = (Button)sender;
if (myButton != null)
{
var test = myButton.Tag;
switch (test)
{
case 1: MessageBox.Show("button 1 was clicked");
break;
case 2: MessageBox.Show("button 2 was clicked");
break;
case 3: MessageBox.Show("button 3 was clicked");
break;
}
}
}
if the upper mthode is succesful the object has to be a button (casted sender into button -> must be a button) But now i have problems to adress the tag. Can somebody help me? I dont find something on the internet and i´m sorry if this is a stupid question, but im new to c#.
You can try with
Id property