简体   繁体   中英

how to disable the button color in c#

I have a set of 5 buttons which I created in winforms using c#,if i click the button1 it should change to green color.,then if button2 is clicked then it should change to green.,but button1 should change to its original color.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            {
                button1.BackColor = Color.Green;
                button2.BackColor = Color.Lavender;
                button3.BackColor = Color.Lavender;
                button4.BackColor = Color.Lavender;
                button5.BackColor = Color.Lavender;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Lavender;
            button2.BackColor = Color.Green;
            button3.BackColor = Color.Lavender;
            button4.BackColor = Color.Lavender;
            button5.BackColor = Color.Lavender;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Lavender;
            button2.BackColor = Color.Lavender;
            button3.BackColor = Color.Green;
            button4.BackColor = Color.Lavender;
            button5.BackColor = Color.Lavender;
        }
    }
}

but this code will be hectic if i have many buttons.,please help me out of this.

You can create a single event handler and assign it to Click event of all your buttons:

private void ButtonClickHandler(object sender, EventArgs e)
{
    // iterate over all buttons on form
    foreach (var button in Controls.OfType<Button>())
        button.BackColor = button == sender ? Color.Green : Color.Lavender;
}

As @CharlesMay stated in comment, be aware, that this code will find each and any Button that is direct child of your form. There is some ways to avoid it:

1. Hold you buttons in a container. For example create a Panel (say myPanel ) and place all of this buttons on that panel, then iterate over that panel's controls:

foreach (var button in myPanel.Controls.OfType<Button>())

2. Store you active button in a private field. This way you don't need to iterate over controls at all:

private Button _activeButton = null;

private void ButtonClickHandler(object sender, EventArgs e)
{
    // disable previosly active button
    if (_activeButton != null) _activeButton.BackColor = Color.Lavender;
    // set new button
    _activeButton = sender as Button;
    // enable currently active button
    if (_activeButton != null) _activeButton.BackColor = Color.Green;
}

You could do it like this:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void SetActive(Button active)
        {
            foreach(var btn in new[] {button1, button2, button3, button4, button5})
            {
               btn.BackColor = btn == active ? Color.Green : Color.Lavender;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetActive(button1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetActive(button2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SetActive(button3);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM