How do I provide a foolproof auto-dropdown for a combo box receiving the focus?

RobertAlanGustafsonII

Active member
Joined
Sep 16, 2023
Messages
41
Programming Experience
10+
WHAT I HAVE:
Visual Basic2019/ 2022, WinForms, .NET Framework 4.6.1 and up

MY PROBLEM:
I'm trying to come up with code that causes the list of a combo box to drop down when the control gets the focus. The simplest way is to include the following code in either the GotFocus or Enter event procedure:

Auto-dropdown code:
If Not control.DroppedDown Then
    control.DroppedDown = True
End If

The code works fine when one tabs to the control. When one clicks on the control, however, the list usually (but not always) drops down then closes up again. How do I fix this issue? Please provide any answer ASAP, in VB.NET, and as simply as possible.
 
Last edited:
I see the open/close happens when style is DropDown and clicking the combobox button, or when style is DropDownList and clicking anywhere. In both cases the control itself is dropping, and the code interferes with this. I suggest yielding the Enter event and see if manually dropping is necessary after that:
VB.NET:
Private Async Function YieldDropDown(cb As ComboBox) As Task
    Await Task.Yield
    If Not cb.DroppedDown Then
        cb.DroppedDown = True
    End If
End Function
Then in Enter event call
VB.NET:
Dim unused = YieldDropDown(ComboBox1)
YieldDropDown is awaitable if you need to do more in Enter event after this call.
 
Back
Top