简体   繁体   English

当鼠标离开容器时关闭Winform MenuStrip

[英]Closing Winform MenuStrip when mouse leaves the container

I´m have been dealing with this issue all day. 我整天都在处理这个问题。 I have a menustrip with several menu items. 我有一个带有几个菜单项的菜单条。 Each item, also have many items. 每个项目,也有很多项目。 Now, if I click the parent menu item, a container(similar to contextmenu) stays opened until I click somewhere else. 现在,如果单击父菜单项,则一个容器(类似于contextmenu)将保持打开状态,直到我单击其他位置为止。

在此处输入图片说明

In the image example, "Clientes" will be the parent item, and there its the "container" with their child menu items. 在图像示例中,“ Clientes”将是父项,那里是带有其子菜单项的“容器”。

What I wanted to do, is to close that "container" when the mouse leaves the parent item or the container area. 我想做的是,当鼠标离开父项或容器区域时,关闭“容器”。 I dont want to have to click in any other part of the form to close it. 我不想单击表格的其他任何部分以将其关闭。 When I say that, its because I´m actually using WPF buttons and I need to do it this way. 当我这么说时,这是因为我实际上正在使用WPF按钮,所以我需要这样做。

I need help here, hope somebody can give me some advice. 我在这里需要帮助,希望有人能给我一些建议。

I tried to use the MouseLeave event of the parents items ("Clientes", etc.) and there did: 我尝试使用父项(“ Clientes”等)的MouseLeave事件,并且做到了:

private void clientesToolStripMenuItem_MouseLeave(object sender, EventArgs e)
    {
        clientesToolStripMenuItem.DropDown.Close();
    }

This works, but obviously, it closes the parent and also the container, when the mouse leaves menuitem "Clientes". 这是可行的,但是很明显,当鼠标离开菜单项“ Clientes”时,它关闭了父级和容器。 I need a way to know if the mouse is over the parent menuitem or over one of their childs so as to close it if I know that the mouse is somewhere else. 我需要一种方法来确定鼠标是否位于父菜单项之上或位于其子项之上,以便在我知道鼠标位于其他位置时将其关闭。

Hope somebody can help me out. 希望有人可以帮助我。

You can achieve this by handling events of clientesToolStripMenuItem itself. 您可以通过处理clientesToolStripMenuItem本身的事件来实现。 When the mouse enters your clients main-menu rectangle, just show your container and hide it when it leaves this rectangle. 当鼠标进入您的客户主菜单矩形时,只需显示您的容器并在离开该矩形时将其隐藏即可。 Assuming picContainer is your rectangle, you may do something like this: 假设picContainer是您的矩形,则可以执行以下操作:

private void clientToolStripMenuItem_MouseEnter(object sender, EventArgs e)
{
    picContainer.Location = clientToolStripMenuItem.ContentRectangle.Location;
    picContainer.Show();
}

private void clientToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
    this.picContainer.Hide();
}

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

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