简体   繁体   English

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

[英]How do I close a toolstripmenuitem that is set to autoclose = false?

I have a menu of items that the user can toggle. 我有一个菜单项,用户可以切换。 I wanted the menu to stay open so the user can check all the items they want. 我希望菜单保持打开状态,以便用户可以检查他们想要的所有项目。 I set autoclose = false and now that works great. 我设置autoclose = false,现在效果很好。 However, I also cannot close the window now lol. 但是,我现在也无法关闭窗口。 I tried clicking off of the menu onto the form, hitting escape, hitting the menu item, hitting the keycombo for the menu, nothing works. 我尝试单击菜单上的窗体,单击转义,单击菜单项,单击菜单的组合键,但没有任何效果。

Ideally, I'd like the user to be able to just click the form or basically anything but the menu to close it or press escape. 理想情况下,我希望用户能够单击表单或除菜单以外的几​​乎所有内容来关闭它或按Escape键。 How would I accomplish that? 我该怎么做? I tried creating a gotfocus event on the form and doing item.HideDropDown in there but no dice. 我尝试在窗体上创建一个getfocus事件并在其中执行item.HideDropDown,但没有骰子。

Thanks! 谢谢!

Generate the click event for the form, and then go through and for every control that doesn't have its own click event, set its click event to the one for the form. 生成表单的click事件,然后遍历每一个没有自己的click事件的控件,将其click事件设置为表单的click事件。

In the event, include the code to hide the menu: toolStripDropDownButton.HideDropDown(); 在这种情况下,请包含隐藏菜单的代码: toolStripDropDownButton.HideDropDown();

Copy the code to any existing click events for other controls. 将代码复制到其他控件的任何现有单击事件。

This is how I handled hiding a monthcalendar when you click anywhere on the form. 当您单击表单上的任意位置时,这就是我如何隐藏月历的方式。

And if you want to also include pressing escape as an option, do the same thing with a KeyDown event, checking if it's the escape key before running the code. 而且,如果您还希望包含按转义的选项,请对KeyDown事件执行相同的操作,并在运行代码之前检查它是否为转义键。

I had similar problem, and here is my solution. 我有类似的问题,这是我的解决方案。 I created common MouseEnter and MouseLeave event handlers and used a timer to delay-close the menu after mouse leaves it. 我创建了通用的MouseEnter和MouseLeave事件处理程序,并使用计时器在鼠标离开菜单后延迟关闭菜单。

Below is a sample code for the menu of 3 items and 1 separator. 以下是包含3个项目和1个分隔符的菜单的示例代码。 In the sample 2 items work with AutoClose and one (the _modeChangingItem) does not close the menu. 在示例中,有2个项目可使用“自动关闭”功能,而其中一个(_modeChangingItem)不会关闭菜单。 You can easily customize this for your needs, eg make none of the items AutoClose. 您可以轻松地根据自己的需要对此进行自定义,例如,不选择“自动关闭”。

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

    [...]
}

This solution saves extra click for user - the timer closes the menu when you move mouse outside of all the items. 该解决方案为用户节省了额外的点击时间-当您将鼠标移到所有项目之外时,计时器关闭菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM