tags:

views:

200

answers:

3

Is there a way to have a ToolStripMenuItem not closing when I click a child control (in its DropDrowItems Collection)?

In my case, I have some ToolStripMenuItems that work as a check box. Actually, I implemented a radio behavior in some ToolStripMenuItems using their Check property. But I don't want the menu closing when I click any of them, because they aren't an action, they represent just options in the menu item.

Is that possible?

+1  A: 
   this.menuItem.DropDown.Closing += new ToolStripDropDownClosingEventHandler(DropDown_Closing); 

void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
            {
                if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
                {
                    e.Cancel = true;
                    ((ToolStripDropDownMenu) sender).Invalidate();
                } 
            }
zabulus
A: 

Look for ToolStripDropDown.AutoClose property.

arbiter
A: 

Just for your information:

  • The Closing event exists on the ContextMenuStrip and the ToolStripDropDown.
  • While designing with the designer, you have access to the ContextMenuStrip (the popup menu) and the ToolStripMenuItem (a submenu) objects, but not the ToolStripDropDown objects of the ToolStripMenuItem!
  • That's why you must set the Closing event of the ToolStripDropDown objects yourself by code (see Zabulus answer).
Kroah