4

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

ArMaN
  • 2,306
  • 4
  • 33
  • 55
  • 2
    Could you make an picture or something? Because as far as I understand it, you can't click on a item from a toolstripmenu that isn't 'opened'. So basically I don't understand. – Kilazur Feb 04 '15 at 08:27
  • @Kilazur I Opened toolstripmenu and click on its items, when I click on a item the toolstriped menu closed automatically. I don't want it hides after clicking. – ArMaN Feb 04 '15 at 08:31
  • Then when you want it to close? and how? – Sriram Sakthivel Feb 04 '15 at 08:48
  • @SriramSakthivel it close when I click on a exit button for example. – ArMaN Feb 04 '15 at 08:52
  • Ok, post the code of what you have. because there is nothing called Toolstripmenu. Which class you're referring to? – Sriram Sakthivel Feb 04 '15 at 08:59
  • @SriramSakthivel question edited. – ArMaN Feb 04 '15 at 09:04
  • 1
    Does that `ToolStripMenuItem` is added to `ToolStrip` or `ContextMenuStrip`? If latter it is easy to do what you're asking. – Sriram Sakthivel Feb 04 '15 at 09:07
  • Forcing a common UI element to behave in strange ways will only confuse and annoy users. Reconsider your goal and if you are using the right tools for the job. – DonBoitnott Feb 04 '15 at 12:53

2 Answers2

8

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();
LarsTech
  • 80,625
  • 14
  • 153
  • 225
8

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;
    }
}
maxc137
  • 2,291
  • 3
  • 20
  • 32