开发者

How can I programmatically close a ComboBox drop down in VB.NET?

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 mes开发者_StackOverflow中文版sage. 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?

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


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));
    }

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


This solved the issue for me:

private void comboBox_DropDown(object sender, EventArgs e)
{
    Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => this.comboBox.DroppedDown = false));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜