The items collection of the ToolStrip and the derived controls, MenuStrip, ContextMenuStrip, StatusStrip is the ToolStripItemCollection which has a Find method for the deep search just like the ControlCollection.Find method. So you have to search this collection through the Items properties of the mentioned controls for a ToolStripItem or any derived type.
Create a search function for the mentioned classes:
Public Function GetToolStripItem(toolStrip As ToolStrip, key As String) As ToolStripItem
Return toolStrip.Items.Find(key, True).FirstOrDefault
End Function
... and call it as follows:
'Search a MenuStrip
Dim tsi = GetToolStripItem(MenuStrip1, key)
'Search a ToolStrip
Dim tsi = GetToolStripItem(ToolStrip1, key)
'Search a ContextMenuStrip
Dim tsi = GetToolStripItem(ContextMenuStrip1, key)
'Search a StatusStrip
Dim tsi = GetToolStripItem(StatusStrip1, key)
If tsi IsNot Nothing Then
tsi.Enabled = False
End If
Also, you can use the TypeOf operator to determine the type of an item:
If TypeOf tsi Is ToolStripMenuItem Then
'...
End If
If TypeOf tsi Is ToolStripDropDownItem Then
'...
End If
If TypeOf tsi Is ToolStripButton Then
'...
End If
... and use the iterator functions to get all or a specific type of items from the collections:
Public Iterator Function GetAllToolStripItems(items As ToolStripItemCollection) As _
IEnumerable(Of ToolStripItem)
For Each tsi As ToolStripItem In items
Yield tsi
If TypeOf tsi Is ToolStripDropDownItem Then
For Each ddi As ToolStripItem In
GetAllToolStripItems(DirectCast(tsi, ToolStripDropDownItem).DropDownItems)
Yield ddi
Next
End If
Next
End Function
Public Iterator Function GetAllToolStripItems(Of T)(items As ToolStripItemCollection) As _
IEnumerable(Of T)
For Each tsi In items
If TypeOf tsi Is T Then
Yield DirectCast(tsi, T)
ElseIf TypeOf tsi Is ToolStripDropDownItem Then
For Each ddi In
GetAllToolStripItems(Of T)(DirectCast(tsi, ToolStripDropDownItem).DropDownItems)
Yield ddi
Next
End If
Next
End Function
... and the usage:
'Get them all...
Dim items = GetAllToolStripItems(TooStrip1.Items) 'or any derived control...
'Get for example the ToolStripComboBox items...
Dim items = GetAllToolStripItems(Of ToolStripComboBox)(MenuStrip1.Items)
Note that in the iterators, identifying the ToolStripDropDownItem is necessary to get the ToolStripItemCollection of derived classes including:
Each of which inherits the DropDownItems property which of course should be passed to the iterator.