RobinS ha scritto:
I'm going to ask a stupid question here. When you say context
menus, you mean the menus that show up when you right-click on
something, right?
Is that's how microsoft call them
So this code will go through all the menu items on a form,
whether it be in a menu strip at the top, a tool stip at the
top, or a context menu assigned to a control, right?
Cooooool.
I hope it is. But I have written too many lines of codes to not be
absolutely sure this code written so hastily contains somewhere some
bugs
)
Anyway this is the form in which I am going to use it. Since I may need
to use it in several places of my programs it's just the case to have a
reusable class.
Here is a sample of use. Please let me know when discover errors ...
Public Class Form1
'EXAMPLE OF USE OF THE "MENU RECURSOR"
'=====================================
'if anyone uses this and finds bugs or improvements I'd love to
know: (e-mail address removed)
Private WithEvents MenuRecursor As New MenuRecursor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.MenuRecursor = New MenuRecursor
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
With Me.MenuRecursor
'This visit all the items of All the MenuStrip's in all
controls
'or subcontrols (got first) of this form
.RecurseMenuStrip(Me)
'This visit all the items of All the ContextMenuStrip's in
all controls
'or subcontrols of this form
.RecurseContextMenuStrip(Me)
End With
End Sub
Sub HandleFoundToolStripMenuItem(ByVal ToolStripItem As
ToolStripItem) Handles MenuRecursor.FoundToolStripMenuItem
MsgBox("I am on " & ToolStripItem.Name)
End Sub
End Class
Public Class MenuRecursor
Public Event FoundToolStripMenuItem(ByVal ToolStripItem As
ToolStripItem)
Sub RecurseMenuStrip(ByVal Control As Control)
For Each ControlChild As Control In Control.Controls
If TypeOf ControlChild Is MenuStrip Then
Dim MenuStrip As MenuStrip = DirectCast(ControlChild,
MenuStrip)
Me.RecurseToolStripItems(MenuStrip.Items)
Else
Me.RecurseMenuStrip(ControlChild)
End If
Next ControlChild
End Sub
Sub RecurseContextMenuStrip(ByVal Control As Control)
Dim FieldInfo As System.Reflection.FieldInfo =
Control.GetType.GetField("components", _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic)
'for context menu in control components
If FieldInfo IsNot Nothing Then
Dim ControlComponents As System.ComponentModel.IContainer =
DirectCast(FieldInfo.GetValue(Control),
System.ComponentModel.IContainer)
If ControlComponents IsNot Nothing Then
For Each ControlChild As Control In
ControlComponents.Components
If TypeOf ControlChild Is ContextMenuStrip Then
Dim ContextMenuStrip As ContextMenuStrip =
DirectCast(ControlChild, ContextMenuStrip)
Me.RecurseToolStripItems(ContextMenuStrip.Items)
Else
Me.RecurseContextMenuStrip(ControlChild)
End If
Next ControlChild
End If
End If
'for possible context menus in custom controls
For Each ControlChild As Control In Control.Controls
Me.RecurseContextMenuStrip(ControlChild)
Next ControlChild
End Sub
Sub RecurseToolStripItems(ByVal ToolStripItemCollection As
ToolStripItemCollection)
For Each ToolStripItem As ToolStripItem In
ToolStripItemCollection
RaiseEvent FoundToolStripMenuItem(ToolStripItem)
If TypeOf ToolStripItem Is ToolStripMenuItem Then
Dim ToolStripMenuItem As ToolStripMenuItem =
DirectCast(ToolStripItem, ToolStripMenuItem)
Me.RecurseToolStripItems(ToolStripMenuItem.DropDownItems)
End If
Next ToolStripItem
End Sub
End Class