有没有办法在您单击菜单时阻止它关闭? [英] Is there a way to stop a Menu from closing when you click on it?

查看:30
本文介绍了有没有办法在您单击菜单时阻止它关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的程序中有一个表单,允许用户通过使用菜单栏来确定用户在设置权限时可以看到的内容.当用户点击一个项目时,该项目就会被选中"(在它旁边打勾).但是,这也会关闭菜单.

We have a form in our program that allows a user to determine what a user can see when setting privileges, by using a menu bar. When a user clicks on an item, that item then is "selected" (gets a tick next to it). However, this also closes the menu.

当用户点击这个菜单时,有没有办法阻止它关闭(而不影响程序中的任何其他菜单)?到目前为止,我还没有在设置中找到任何东西,任何 _click 方法也没有影响它.

Is there a way to stop this menu from closing (without affecting any other menu's in the program) when a user clicks on it? So far I have not found anything in the settings, and any _click methods are not affecting it either.

推荐答案

聆听 ToolStripDropDownClosingEventHandler 存在于 ToolStripDropDown.为此,只需访问 ToolStripMenuItem.DropDown 属性,然后添加一个将处理 Closing 事件的侦听器.我们只需要检查鼠标指针是否在 toolStrip 矩形内,这可以用这段代码完成:

Listen the ToolStripDropDownClosingEventHandler present in ToolStripDropDown. To do this just access the ToolStripMenuItem.DropDown Property, and so, add a listener that will handle the Closing event. We just need to check if the mouse pointer it's inside of toolStrip rectangle and this can be done with this piece of code:

private void ListenToolStripMenuItems(IEnumerable<ToolStripMenuItem> menuItems)
{
    // listen all menuItems
    foreach (ToolStrip menuItem in menuItems)
        menuItem.DropDown.Closing += OnToolStripDropDownClosing;
}

private void OnToolStripDropDownClosing(object sender, ToolStripDropDownClosingEventArgs e)
{
        var tsdd = (ToolStripDropDown)sender;

        // checking if mouse cursor is inside
        Point p = tsdd.PointToClient(Control.MousePosition);  
        if (tsdd.DisplayRectangle.Contains(p))
            e.Cancel = true;  // cancel closing
}

这样 AutoClose 仍然有效,toolStrip 将正常关闭.

This way the AutoClose still working and the toolStrip will close properly.

这篇关于有没有办法在您单击菜单时阻止它关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆