gsb
gsb

Reputation: 1229

ToolStripMenuItem not closing when a child ToolStripMenuItem is Clicked in a C# WinForm

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?

Upvotes: 3

Views: 3096

Answers (3)

Kroah
Kroah

Reputation: 1

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).

Upvotes: 0

arbiter
arbiter

Reputation: 9575

Look for ToolStripDropDown.AutoClose property.

Upvotes: 1

zabulus
zabulus

Reputation: 2513

   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();
                } 
            }

Upvotes: 4

Related Questions