Tech Blog‎ > ‎

VB.NET - DropDownOpening Event For ComboBox

posted Feb 21, 2012, 11:57 AM by Victor Zakharov   [ updated Mar 15, 2014, 7:15 PM ]
Here is a proper way to do it - inherit a class from native ComboBox and override OnDropDown to raise a custom DropDownOpening event first:

Public Class MyComboBox : Inherits ComboBox
  Public Event DropDownOpening(sender As Object, e As System.EventArgs)

  Protected Overrides Sub OnDropDown(e As System.EventArgs)
    RaiseEvent DropDownOpening(Me, e)
    MyBase.OnDropDown(e)
  End Sub
End Class


Private Sub MyComboBox1_DropDownOpening(sender As Object, e As System.EventArgs) Handles MyComboBox1.DropDownOpening
  'reload your list here
  '...
End Sub

If you anticipate loading to take considerable time, you can wrap your reload code into a construct like this:

Dim oldCursor As Cursor = MyComboBox1.Cursor
MyComboBox1.Cursor = Cursors.WaitCursor
'reload your list here
'...
MyComboBox1.Cursor = oldCursor

(DoEvents is not needed to propagate cursor change)