2009-03-03

Display the drop down list of Combo box Explicitly

When you set the DropDownStyle property of Combo box, it allows you input the text like text box control. When you type something and press down arrow key then the item from the list which starts from the text entered is selected. Imagine, how helpful it would be if we can see the drop down list while typing. Below is the code which does exactly the same.

In this code I am displaying the drop down list on GotFocus event of ComboBox.

Imports System.Runtime.InteropServices

Public Class Form1

_
Private Shared Function SendMessage( _
ByVal hWnd As IntPtr, _
ByVal Msg As UInteger, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function

Public Const CB_SHOWDROPDOWN As Long = &H14F

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

Dim comboItems() As String
comboItems = New String() {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}

With ComboBox1
.Items.Clear()
.Items.AddRange(comboItems)
End With
End Sub

Private Sub ComboBox1_GotFocus( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles ComboBox1.GotFocus

Dim iret As IntPtr
iret = SendMessage(ComboBox1.Handle, CB_SHOWDROPDOWN, New IntPtr(CInt(True)), IntPtr.Zero)
End Sub

End Class