本文介绍了工具条(ToolStripDropDownButton)关闭并失去窗口焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ToolStripDropDownButton的ToolStrip.依次弹出带有DateTimePicker的UserControl.当我单击日历上的顶部数字(绿色)时,一切正常(用户控件保持打开状态).当我单击较低日期之一(红色)时,ToolStripDropDown关闭,应用程序失去焦点.这导致用户不得不双击以再次打开DropDown(一次聚焦应用程序,另一次打开下拉菜单).

I have a ToolStrip with a ToolStripDropDownButton. This in turn pop up a UserControl with a DateTimePicker. When I click on the top numbers on the calendar (in green), everything works fine (user control stay open). When I click on one of the lower dates (in red), the ToolStripDropDown closes and the application loses focus. This causes the user to have to double click to open up the DropDown again (one time to focus the application, an other to open up the drop down).

当日历日期不在用户控件顶部时,会发生这种情况.如您所见,用户控件停止在绿色"日期.

This happens when the calender date are not on top of the user control. As you can see, the user control stops on the "green" dates.

我希望这很清楚.这似乎是Windows错误,因为这是通过非常简单的项目发生的.有没有人看过这个问题?有解决办法吗?

I hope this is clear. This seems to be a windows bug since this happens with a very simple project. Have anyone seens this problem? Is there a fix for this?

[UPDATE]

我在下拉菜单中看到了同样的事情.

I notice the same thing with dropdown.

用户控件很大时不会发生这种情况.

It doesn't happen with the user control is very big.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim uc As New UserControl1
    Dim cms As New ContextMenuStrip()
    cms.Items.Add(New ToolStripControlHost(uc))
    ToolStripButton1.DropDown = cms

End Sub

推荐答案

功能,而不是错误.该控件需要知道用户单击了下拉列表之外的位置,以便随后可以自动关闭该下拉列表.这是通过鼠标捕获"完成的,该捕获在Winforms中由Capture属性公开.捕获鼠标可以确保将鼠标消息发送到控件,即使鼠标不再位于控件窗口中也是如此.

Feature, not a bug. The control needs to know that the user clicked outside of the dropdown so it can then automatically close the dropdown. That's done with "mouse capture", exposed in Winforms by the Capture property. Capturing the mouse ensures that mouse messages are sent to the control, even if the mouse is no longer located inside the control window.

也许很明显,现在发生了什么,实际上您没有单击日历.显示下拉列表后,控件将捕获鼠标.单击将重定向到ToolStripDropDown.当您单击红色区域"时,可以看到鼠标不再位于下拉列表中,从而关闭了下拉窗口.

Perhaps it is obvious what's happening now, you are not in fact clicking on the calendar. Once the dropdown is displayed, the control captures the mouse. The click is redirected to the ToolStripDropDown. Which sees that the mouse is no longer inside the dropdown when you click in the "red area" and thus closes the dropdown window.

这样的下拉列表实际上仅适用于简单的项目,不支持将创建顶部窗口的任何控件放入其中,例如DateTimePicker或ComboBox.

Dropdowns like this are really only suitable for simple items, putting any control inside of it that creates a toplevel window, like DateTimePicker or ComboBox just isn't a supported scenario.

这篇关于工具条(ToolStripDropDownButton)关闭并失去窗口焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:32