如何以编程方式关闭 VB.NET 中的 ComboBox 下拉列表? [英] How can I programmatically close a ComboBox drop down in VB.NET?

查看:24
本文介绍了如何以编程方式关闭 VB.NET 中的 ComboBox 下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 窗体应用程序 (VB.NET) 中,我有一个组合框,在某些情况下,当用户打开下拉列表时,我想显示一条警告消息.所有这些都运行良好:在 DropDown 事件中,我检查条件,如果需要,我会显示警告消息.问题是,当用户在警告消息上单击 Yes 时,我移动到主窗体上的另一个选项卡,即使我已经离开包含组合框的选项卡,下拉列表仍然显示.我已经搜索了很多,并试图将 DroppedDown 设置为 False,调用 OnDropDownClosed 等,但没有任何效果.似乎当我尝试从 DropDown 事件中关闭下拉列表时,它不起作用.任何人都可以提出任何建议吗?

In a Windows Form application (VB.NET) I have a combo box and under certain conditions, when the user opens the DropDown list, I want to display a warning message. All of this works well: In the DropDown event, I check the conditions and if required I display the warning message. The problem is that when the user clicks on Yes on the warning message, I move to another tab on the main form and the drop down list still shows even though I've moved away from the tab containing the combo box. I've searched quite a bit and tried to set DroppedDown to False, call OnDropDownClosed, etc but nothing worked. It seems that when I try to close the drop down list from within the DropDown event, it doesn't work. Can anyone suggest anything?

感谢到目前为止的答案.我忘了说我使用的是 .NET framework 2.

Thanks for the answers so far. I forgot to mention that I use .NET framework 2.

推荐答案

这里的问题是 WinForms 将鼠标捕获"到下拉列表之后它引发了事件.因此,在您的事件处理程序中,您无法阻止下拉列表.您可以做的是安排鼠标取消捕获".

The problem here is that WinForms 'captures' the mouse to the dropdown after it has raised the event. So within your event handler, there is nothing you can do to prevent the dropdown. What you can do though is schedule the mouse to 'uncapture'.

    private void comboBox1_DropDown(object sender, EventArgs e)
    {
        Dispatcher.CurrentDispatcher.BeginInvoke((Action)(()=>comboBox1.Capture=false));
    }

在 .NET 4 中,Dispatcher 位于 System.Windows.Threading 命名空间下的 WindowsBase 程序集中.

In .NET 4, Dispatcher lives in the WindowsBase assembly under the System.Windows.Threading namespace.

这篇关于如何以编程方式关闭 VB.NET 中的 ComboBox 下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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