Do not close ToolStripMenu on clicking in winforms

亡梦爱人 提交于 2021-01-27 15:19:25

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/28316674/do-not-close-toolstripmenu-on-clicking-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!