Toolstrip with check button group

Tom picture Tom · Jun 17, 2011 · Viewed 16.5k times · Source

I'd like to have a toolstrip with some buttons on it (WinFroms/c#/.net4). I'd like to have the clicked button be checked and all others unchecked, so I want to have only the clicked button checked, all the others unchecked.

I know toolstrip button has checkedonlclick property, but when I click one button, others may be also checked. In good old VB6 there was an automatic solution for this problem: buttongroup on toolbar. Is there any similar in Winfoms?

Or should I handle it from code?! Switch all other buttons to unchecked state when one button is checked? If so, then it would not be my favourite solution...

Answer

KreepN picture KreepN · Jun 17, 2011

Call this code on the toolStripButton_Click events and you should get the desired result.

   foreach (ToolStripButton item in ((ToolStripButton)sender).GetCurrentParent().Items)
   {
       if (item == sender) item.Checked = true;
       if ((item != null) && (item != sender))
       {
          item.Checked = false;
       }
   }