VB .NET • Redisplay Menu Items

Listing 2. Looping through the menu items lets you redisplay previously hidden items. Suspending and resuming the layout of the dropdown ensures smooth visual behavior without flashing. The class-level field inExpandedClick tells the OnDropDownShow method not to hide the methods again immediately.

Private Sub ExpandItems()
	Me.DropDown.SuspendLayout()
	For Each item As ToolStripItem In _
		Me.DropDownItems
		Dim menuItem As SpecialMenuItem = _
			TryCast(item, SpecialMenuItem)
		If menuItem IsNot Nothing Then
			If menuItem.Visible = False And Not _
				menuItem.AlwaysHidden Then
				menuItem.Visible = True
			End If
		End If
		Dim separator As ToolStripSeparator = _
			TryCast(item, ToolStripSeparator)
		If separator IsNot Nothing Then
			separator.Visible = True
		End If
	Next

	inExpandedClick = True
	Me.ShowDropDown()
	inExpandedClick = False
	ResetMenu()
	Me.DropDown.ResumeLayout()
End Sub