ArMaN
ArMaN

Reputation: 2407

Do not close ToolStripMenu on clicking in winforms

I work on a c# winform project that the main toolstripmenu have not to be hide after user clicks on its item, how can I do that?

enter image description here

Upvotes: 4

Views: 4791

Answers (2)

maxc137
maxc137

Reputation: 2771

I found better answer on MSDN forum. Dropdown doesn't close on click, but closes in other cases:

DropDown.Closing += new ToolStripDropDownClosingEventHandler(DropDown_Closing);
...
private void DropDown_Closing(object sender,  ToolStripDropDownClosingEventArgs e)
{
    if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
    {
        e.Cancel = true;
    }
}

Upvotes: 9

LarsTech
LarsTech

Reputation: 81675

Set the AutoClose property of the parent menu item to prevent the menu strip from closing.

To demonstrate:

ToolStripMenuItem file = new ToolStripMenuItem("File");
file.DropDown.AutoClose = false;
file.DropDownItems.Add("New");
file.DropDownItems.Add("Open");
file.DropDownItems.Add("Exit");

MenuStrip ms = new MenuStrip();
ms.Items.Add(file);

this.Controls.Add(ms);

Now the responsibility is on you to close the menu yourself:

file.DropDown.Close();

Upvotes: 8

Related Questions