如何关闭设置为 autoclose = false 的toolstripmenuitem?

问题描述 投票:0回答:3

我有一个用户可以切换的项目菜单。我希望菜单保持打开状态,以便用户可以检查他们想要的所有项目。我设置 autoclose = false 现在效果很好。但是,我现在也无法关闭窗口哈哈。我尝试从菜单中单击到窗体上,单击“转义”,单击菜单项,单击菜单的组合键,但没有任何效果。

理想情况下,我希望用户能够只需单击表单或基本上除菜单之外的任何内容即可将其关闭或按转义键。我将如何实现这一目标?我尝试在表单上创建一个 gotfocus 事件并在那里执行 item.HideDropDown 但没有骰子。

谢谢!

c# winforms toolstripdropdown toolstripmenu
3个回答
3
投票

为表单生成单击事件,然后遍历每个没有自己的单击事件的控件,将其单击事件设置为表单的单击事件。

如果发生,请添加隐藏菜单的代码:

toolStripDropDownButton.HideDropDown();

将代码复制到其他控件的任何现有单击事件。

这就是当您单击表单上的任意位置时我处理隐藏月历的方法。

如果您还想将按转义键作为选项包含在内,请对 KeyDown 事件执行相同的操作,在运行代码之前检查它是否是转义键。


0
投票

我有类似的问题,这是我的解决方案。 我创建了常见的 MouseEnter 和 MouseLeave 事件处理程序,并使用计时器在鼠标离开菜单后延迟关闭菜单。

下面是包含 3 个项目和 1 个分隔符的菜单的示例代码。在示例中,有 2 个项目与 AutoClose 一起使用,其中一个项目(_modeChangingItem)不关闭菜单。您可以根据您的需求轻松定制它,例如不让任何项目自动关闭。

private Timer _menuTimer = new Timer();

private void MainFrm_Load (object sender, EventArgs e)
{
    _menuTimer.Interval = 200;
    _menuTimer.Tick += _menuTimer_Tick;

    _rootMenuItem.MouseEnter += commonMenu_MouseEnter;
    _rootMenuItem.MouseLeave += commonMenu_MouseLeave;

    _menuItem1.MouseEnter += commonMenu_MouseEnter;
    _menuItem1.MouseLeave += commonMenu_MouseLeave;
    _menuItem2.MouseEnter += commonMenu_MouseEnter;
    _menuItem2.MouseLeave += commonMenu_MouseLeave;
    _separator.MouseEnter += commonMenu_MouseEnter;
    _separator.MouseLeave += commonMenu_MouseLeave;
    _modeChangingItem.MouseEnter += commonMenu_MouseEnter;
    _modeChangingItem.MouseLeave += commonMenu_MouseLeave;

}

private void commonMenu_MouseLeave(object sender, EventArgs e)
{
    _menuTimer.Stop();

    // Comment this line out if you want none of the items to AutoClose 
    _rootMenuItem.DropDown.AutoClose = true;

    ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
    if (menuItem != null) menuItem.Tag = null;
    ToolStripSeparator separator = sender as ToolStripSeparator;
    if (separator != null) separator.Tag = null;
    _menuTimer.Start();
}

private void commonMenu_MouseEnter(object sender, EventArgs e)
{
    ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
    if (menuItem != null) menuItem.Tag = new object();
    ToolStripSeparator separator = sender as ToolStripSeparator;
    if (separator != null) separator.Tag = new object();
}

private void _menuTimer_Tick(object sender, EventArgs e)
{
    if (_rootMenuItem.Tag == null && _menuItem1.Tag == null &&
                                     _menuItem2.Tag == null &&
                                     _separator.Tag == null &&
                                     _modeChangingItem.Tag == null)
    {
        _rootMenuItem.DropDown.Close();
    }
    _menuTimer.Stop();
}

private void _modeChangingItem_Click(object sender, EventArgs e)
{
    ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
    if (menuItem == null) return;

    // Move this line to Form_Load if you want none of the items AutoClose 
    _rootMenuItem.DropDown.AutoClose = false; // Now the menu stays opened

    [...]
}

此解决方案为用户节省了额外的点击 - 当您将鼠标移到所有项目之外时,计时器会关闭菜单。


0
投票

使用 .hidedropdown 关闭下拉菜单

© www.soinside.com 2019 - 2024. All rights reserved.