I am working on a form that has lots of buttons. When the user clicks one button the background should change color. If they click another button on the form its background should change color and the previous buttons color should return back to the original color.
I can do this by hard coding in every button but this form has alot of buttons. I am sure there has to be a more efficient way of doing this
I have this so far
foreach (Control c in this.Controls)
{
if (c is Button)
{
if (c.Text.Equals("Button 2"))
{
Btn2.BackColor = Color.GreenYellow;
}
else
{
}
}
}
I can get the background for Btn2 to change. How would i change the background for all the other buttons in the form. Any ideas how i could do this without having to hardcode each button.
The code below will work without regard to the number of buttons on the form. Simply set the
button_Clickmethod to be the event handler of all buttons. When you click on a button, its background will change color. When you click on any other button, that button’s background will change color, and the previously-colored button’s background will revert to the default background color.…