RadioCheck for MenuToolStrip

Today I wanted to code a submenu with menu items that are checked like a radio button group. So once a single menu item has been checked, there is exactly one item checked all the time.

The task proved a bit harder than I expected. The RadioCheck property is no longer present in MenuToolStrip and ToolStripMenuItem as it was in the old MenuItem class.

So time to code one!

Basically the code below assumes you have a ToolStripMenuItem which contains all ToolStripMenuItems that are part of the radio button group in its DropDownItems collection.

Optionally you may check one ToolStripMenuItem as startup. Once a ToolStripMenuItem is checked there is no way to uncheck them all.

Each of these ToolStripMenuItems must have the CheckOnClick property set to true and have a event handler attached like:

   1: tmi.CheckedChanged += new EventHandler(tmi_CheckedChanged);

 

The event handler code below unchecks all other ToolStripMenuItems in the DropDownItems collection in such way that one does not end up with a stack overflow exception.

   1: /// <summary>

   2: /// Simulate RadioCheck MenuItems.

   3: /// </summary>

   4: /// <param name="sender"></param>

   5: /// <param name="e"></param>

   6: void tmi_CheckedChanged(object sender, EventArgs e)

   7: {

   8:     ToolStripMenuItem tmi = (ToolStripMenuItem)sender;

   9:     ToolStripItemCollection items = ((ToolStripMenuItem)(tmi.OwnerItem)).DropDownItems;

  10:  

  11:     //Make sure that one menu item is always checked.

  12:     if (tmi.Checked)

  13:     {

  14:         //Uncheck all other menu items.

  15:         foreach (ToolStripMenuItem tmi2 in items)

  16:         {

  17:             if (!tmi.Equals(tmi2) && tmi2.Checked)

  18:             {

  19:                 tmi2.Checked = false;

  20:             }

  21:         }

  22:     }

  23:     else

  24:     {

  25:         //If anything else is already checked, 

  26:         //bail out to prevent a stack overflow.

  27:         foreach (ToolStripMenuItem tmi2 in items)

  28:         {

  29:             if (tmi2.Checked)

  30:             {

  31:                 return;

  32:             }

  33:         }

  34:  

  35:         //If nothing is checked, check ourselves again.

  36:         tmi.Checked = true;

  37:     }

  38: }

/// <summary>
/// Simulate RadioCheck MenuItems.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tmi_CheckedChanged(object sender, EventArgs e)
{
    ToolStripMenuItem tmi = (ToolStripMenuItem)sender;
    ToolStripItemCollection items = 
((ToolStripMenuItem)(tmi.OwnerItem)).DropDownItems; //Make sure that one menu item is always checked. if (tmi.Checked) { //Uncheck all other menu items. foreach (ToolStripMenuItem tmi2 in items) { if (!tmi.Equals(tmi2) && tmi2.Checked) { tmi2.Checked = false; } } } else { //If anything else is already checked, //bail out to prevent a stack overflow. foreach (ToolStripMenuItem tmi2 in items) { if (tmi2.Checked) { return; } } //If nothing is checked, check ourselves again. tmi.Checked = true; } }
Advertisements
This entry was posted in C#, Programming, Windows. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s