【问题标题】:Do not close ToolStripMenu on clicking in winforms点击winforms时不要关闭ToolStripMenu
【发布时间】:2015-04-03 16:44:05
【问题描述】:

我在一个 c# winform 项目中工作,在用户单击其项目后,主工具条菜单不必隐藏,我该怎么做?

【问题讨论】:

  • 你能拍张照片什么的吗?因为据我了解,您无法从工具条菜单中单击未“打开”的项目。所以基本上我不明白。
  • @Kilazur 我打开了工具条菜单并单击其项目,当我单击一个项目时,工具条菜单会自动关闭。我不希望它在点击后隐藏。
  • 那你想什么时候关闭呢?以及如何?
  • @SriramSakthivel 例如,当我单击退出按钮时它会关闭。
  • ToolStripMenuItem 是否添加到ToolStripContextMenuStrip?如果是后者,很容易按照您的要求进行操作。

标签: c# winforms toolstripmenu


【解决方案1】:

我在MSDN forum 上找到了更好的答案。下拉菜单不会在点击时关闭,但在其他情况下会关闭:

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

【讨论】:

    【解决方案2】:

    设置父菜单项的 AutoClose 属性以防止菜单条关闭。

    演示:

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

    现在你有责任自己关闭菜单:

    file.DropDown.Close();
    

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 2010-12-05
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      相关资源
      最近更新 更多