First, you need to create a new class inherited from ComboBox(full code below). You don't have to override many of the methods. Add a boolean property to help you determine when you want it to drop down. The meat of the functionality is in overriding the OnDrawItem method. Essentially, if your condition (whatever it is) is true, you don't draw any of the items in the combobox. You need to override the OnDropDown method and set the DropDownHeight=1 (0 is invalid), otherwise, the combobox will still dropdown at its normal size, but it will appear to be empty. The combobox still drops down, but you can't see it because its height is only 1 pixel. It's important to set the DrawMode to OwnerDrawFixed in the New method, so the OnDrawItem code is executed.
When you reset the DropDownHeight so the items will display, you can either use a stored value from the original height, or set it to some arbitrarily large value that you know will be larger than you need; the combobox will automatically reduce this height so that it is no larger than necessary to display all the items.
You could simplify things by setting the DrawMode to Normal and ONLY overriding the OnDropDown method, but the OnDrawMethod gives you almost full control over how your list of items is displayed (if that's what you want).
Public Class simpleCombo
Inherits ComboBox
Private _myCondition As Boolean = False
Public Property myCondition() As Boolean
Get
Return _myCondition
End Get
Set(ByVal value As Boolean)
_myCondition = value
End Set
End Property
Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
If _myCondition Then
Me.DropDownHeight = 1
Else
Me.DropDownHeight = 200 //some arbitrarily large value
End If
MyBase.OnDropDown(e)
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
If _myCondition Then
Return
Else
MyBase.OnDrawItem(e)
e.DrawBackground()
e.Graphics.DrawString(Me.Items(e.Index), Me.Font, New SolidBrush(Me.ForeColor), e.Bounds)
e.DrawFocusRectangle()
End If
End Sub
Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
End Sub
End Class