Mark one button and uncheck the rest (Toolstrip)


I know it will be silly, but I've been running around a lot and I'm getting desperate.

I have a windows form with a toolstrip, which in turn contains a series of buttons.

I would like to know how to do so that by pressing one of the buttons on the toolstrip, this button is left marked and the rest of the buttons are automatically unchecked.

Thank you very much everyone!

asked by Javier G. 26.11.2018 в 02:34

1 answer


I do not understand very well what you mean by unchecking, I'm going to give you an example with what I understand of your question. When you click on a button you perform an action, in this case that button changes color, I also want the rest of the buttons to return to their original color, that is, I just want to have an active button. For this I'm going to create a generic method.

This method will go through all my ToolStrip in search of buttons whose name is the same as the one I have clicked, that is, the active one.

   private void Desmarca(string buttonName)
//Recorremos el toolstrip en busca de botones
                foreach (ToolStripButton boton in this.toolStrip1.Items)
// Si el boton no es el mismo que ha sido clickado
                    if (boton.Name != buttonName)
//Tu acción(desmarcar), en mi caso cambiar el color
                        boton.BackColor = Color.Green;

Now what remains to be done is to add to each button the code to call this fragment.

        private void toolStripButton2_Click(object sender, EventArgs e)
//Casteo del sender a objeto de botón
            ToolStripButton B = (ToolStripButton)sender;
//Tu acción, en mi caso cambiar el color
            B.BackColor = Color.Yellow;
//Llamada al fragmento que controla el resto de botones

I hope it can help you. Do not hesitate to clarify your situation in case I can be more specific in your problem.

answered by 26.11.2018 / 10:23