I am tryng to extract all items from a ContextMenuStrip to a List(Of ToolStripMenuItem)
.
I can get the reference of the ContextMenuStrip of a DataGridView, then - with a recursive function - I want to extract the Name
and Text
properties of all the ToolStripMenuItems
, excluding the ToolStripSeparators
, if any.
I use this code:
allItem = New List(Of Control)
Dim lstDbg As List(Of Control) = FindControlRecursive(allItem, form, GetType(DataGridView))
Dim dictRes As New Dictionary(Of String, String)
For Each dbgCtrl As Control In lstDbg
If dbgCtrl.Name <> "" Then
For Each mnuStrip As ToolStripItem In dbgCtrl.ContextMenuStrip.Items
If mnuStrip.GetType() = GetType(ToolStripMenuItem) Then
Dim lstItem As New List(Of ToolStripMenuItem)
Dim lstTS As List(Of ToolStripMenuItem) = FindControlRecursive_ContextMenu(lstItem, mnuStrip, GetType(ToolStripMenuItem))
For Each item As ToolStripMenuItem In lstTS
dictRes.Add(item.Name, item.Text)
Next
End If
Next
End If
Next
The function of recursion is FindControlRecursive_ContextMenu()
:
Public Function FindControlRecursive_ContextMenu(ByVal list As List(Of ToolStripMenuItem), ByVal parent As ToolStripMenuItem, ByVal ctrlType As System.Type) As List(Of ToolStripMenuItem)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As ToolStripMenuItem In parent.DropDown.Items
FindControlRecursive_ContextMenu(list, child, ctrlType)
Next
Return list
End Function
It works, but if in the DropDown list there is a ToolStripSeparator
, I don't have the DropDown.Items
element and the function generates an exception.
How can I skip the ToolStripSeparators and call the recursive function with the next "child"?