VB .NET • Override the OnDropDown Show Method

Listing 1. Overriding the OnDropDownShow method is approximately equivalent to handling the DropDownShow event and lets you alter the appearance of the drop down menu before it appears. To get the Office 2003 expandable menu behavior, you need to make selected items invisible and start a timer.

	ByVal e As System.EventArgs)
	MyBase.OnDropDownShow(e)
	If Not Me.DesignMode Then
		If Not inExpandedClick Then
			Dim hasExpandable As Boolean = False
			Dim itemCount As Int32 = 0
			For Each item As ToolStripItem In _
				Me.DropDownItems
			Dim toolstripMenuItem As ToolStripMenuItem = _
				TryCast(item, ToolStripMenuItem)
		If toolstripMenuItem IsNot Nothing Then
			itemCount += 1
			Dim menuItem As SpecialMenuItem = _
				TryCast(item, SpecialMenuItem)
		If menuItem IsNot Nothing Then
			If Not menuItem.IsDisplayed _
				And Not menuItem.AlwaysHidden Then
					hasExpandable = True
					menuItem.Visible = False
					itemCount -= 1
			End If
		End If
		End If
		Next

		If itemCount < mMinimumItemCount Then
			hasExpandable = Not DisplayMinimumItems()
		End If
		RemoveAdjacentSeparators()
		If hasExpandable Then
			AddExpandMenuItem()
		End If
		End If
	End If
	End Sub

Protected Overrides Sub OnDropDownOpened( _
	ByVal e As System.EventArgs)
	MyBase.OnDropDownOpened(e)
	Me.timer.Enabled = True
End Sub