Alternate checked between buttons I'm trying to alternate if a button is checked. I've come up with this, which works but its feels unnecessary to write 70 lines of code for such a simple task.

How would I go about to shorten this up, is there any way I can call the base toolStripButton and tell it to uncheck other buttons when clicked on and check it self?

private void toolStripButton1_Click(object sender, EventArgs e)
{
    if (toolStripButton1.Checked == true)
    {
        toolStripButton2.Checked = false;
        toolStripButton3.Checked = false;
        toolStripButton4.Checked = false;
        toolStripButton5.Checked = false;
        toolStripButton6.Checked = false;
    }
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
    if (toolStripButton2.Checked == true)
    {
        toolStripButton1.Checked = false;
        toolStripButton3.Checked = false;
        toolStripButton4.Checked = false;
        toolStripButton5.Checked = false;
        toolStripButton6.Checked = false;
    }
}

private void toolStripButton3_Click(object sender, EventArgs e)
{
    if (toolStripButton3.Checked == true)
    {
        toolStripButton1.Checked = false;
        toolStripButton2.Checked = false;
        toolStripButton4.Checked = false;
        toolStripButton5.Checked = false;
        toolStripButton6.Checked = false;
    }
}

private void toolStripButton4_Click(object sender, EventArgs e)
{
    if (toolStripButton4.Checked == true)
    {
        toolStripButton2.Checked = false;
        toolStripButton3.Checked = false;
        toolStripButton1.Checked = false;
        toolStripButton5.Checked = false;
        toolStripButton6.Checked = false;
    }

}

private void toolStripButton5_Click(object sender, EventArgs e)
{
    if (toolStripButton5.Checked == true)
    {
        toolStripButton2.Checked = false;
        toolStripButton3.Checked = false;
        toolStripButton1.Checked = false;
        toolStripButton4.Checked = false;
        toolStripButton6.Checked = false;
    }
}

private void toolStripButton6_Click(object sender, EventArgs e)
{
    if (toolStripButton6.Checked == true)
    {
        toolStripButton2.Checked = false;
        toolStripButton3.Checked = false;
        toolStripButton1.Checked = false;
        toolStripButton5.Checked = false;
        toolStripButton4.Checked = false;
    }

}
Yes, this can easily be achieved with a few non-redundant lines.

You can hook up all button clicks to this common MouseDown event:

private void toolStripButton_MouseDown(object sender, MouseEventArgs e)
{
    ToolStripButton button = sender as ToolStripButton;
    if (button == null) throw new InvalidCastException();
    foreach (ToolStripItem item in button.Owner.Items)
    {   
        if (item is ToolStripButton)
        {
            ((ToolStripButton)item).Checked = (item == button);
        }
    }
}


Note that the Click event doesn't allow you to find out which button was clicked as all button clicks map to the ToolStrip itself! Therefore it doesn't lend itself to any sort of common processing that makes use of the sender paramter all events have..

As ever so often MouseDown works much better ;-)

The logic above assumes you want a radiobutton-like behaviour. RadioButtons can only be switched on; to switch them off you need to click another RadioButton in the group. If you want to be able to switch a button on and off directly you can expand it a little..