Get Button
-
I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?
-
I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?
Have a common (i.e., the same) event-handler for all the buttons on your form, say ButtonClick. Implement this common event-handler as under, for example:
private void ButtonClick(object sender, System.EventArgs e)
{
if((Button)sender == button1)
{
button1.BackColor = Color.Green;
MessageBox.Show("button1 was clicked", "Form1");
}
else if((Button)sender == button2)
{
button2.BackColor = Color.Pink;
MessageBox.Show("button2 was clicked", "Form1");
}
else if((Button)sender == button3)
{
button3.BackColor = Color.Blue;
MessageBox.Show("button3 was clicked", "Form1");
}
else if((Button)sender == button4)
{
button4.BackColor = Color.Yellow;
MessageBox.Show("button4 was clicked", "Form1");
}
}It works properly and identifies the correct button pressed. You can add your own code in place of the sample code above. If the number of buttons is too large, you can even consider having an ArrayList of the buttons.
-
I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?
Hello you don't have to declare 2 string variables here. Try: textBox1.Text = sender.ToString().Substring(35); As to get the button, have you tried: Button myButton = (Button)sender; -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?
The
sender
argument of yourClick
event handler _is_ the button that was pressed. So you can easily cast it toButton
and then do with it whatever you want:private void button_Click(object sender, System.EventArgs e)
{
Button b = sender as Button;if (b != null) // if null the event handler has been bound to a non-Button
{
// Reset all other Buttons
foreach (Control c in this.Controls)
{
if (c is Button)
c.BackColor = SystemColors.Control;
}
// Colorize the button pressed
b.BackColor = Color.Red;
}
}Regards, mav
-
I have a little problem I got a form with a lot off buttons. i use this code to get the Text of the button i pushed: string naam,naam2; naam=sender.ToString(); naam2=naam.Substring(35); textBox1.Text=naam2; But the problem is i want to change the color of that button as well. Could someone tell me how i can determin witch button i pussed with appling just one piece of code to all the buttons?
And no one has suggested it yet... but this might help:
Button btn = (Button) sender; MessageBox.Show(btn.Text); // Display button's caption
I hope this helps! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!