Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is any way to use For Each loop with multiple ContextMenu controls?

What I have tried:

I want to do something like the example bellow...
VB
For Each lbl As Label In Me.Controls
    'Some code here...
Next

But for ContextMenu controls...
VB
For Each cmenu As ContextMenu In ???
Posted
Updated 12-Feb-18 23:03pm

Here is a working solution as I got it from stackoverflow.com here:

If you are using the ContextMenuStrip class which replaces and adds functionality to the ContextMenu control of previous versions, you can use a similar code:
VB
For Each x As Object In Me.components.Components
    If TypeOf x Is ContextMenuStrip Then
        MsgBox(CType(x, ContextMenuStrip).Name)
    End If
Next x
You need to use Reflection in order to find every ContextMenuStrip in the collection of open forms owned by the application.
VB
For Each frm As Form In My.Application.OpenForms
    For Each fi As System.Reflection.FieldInfo In frm.GetType.GetFields(
        System.Reflection.BindingFlags.NonPublic Or _
        System.Reflection.BindingFlags.Instance Or _
        System.Reflection.BindingFlags.DeclaredOnly)
        If fi.FieldType Is GetType(ContextMenuStrip) Then
            MsgBox(frm.Name & " - " & TryCast(fi.GetValue(frm), ContextMenuStrip).Name)
        End If
    Next fi
Next frm
And in case we use a MetroContextMenu control, we should replace every:
ContextMenuStrip
to:
MetroFramework.Controls.MetroContextMenu
 
Share this answer
 
v3
If I recall correctly, each context menu is associated with a particular control, so to get the context menu you still need to iterate over the controls and then you check each control to see if it has a ContextMenu associated with it or not (i.e., the ContextMenu is Nothing).
 
Share this answer
 
Comments
Simos Sigma 12-Feb-18 7:47am    
Do you mean something like this?

For Each FlowLayoutPanel As FlowLayoutPanel In Me.Controls
    For Each lbl As Label In FlowLayoutPanel.Controls
       If lbl.Name = "Blah Blah" Then
          For Each CMenu As ContextMenu In lbl.Controls
             'Code Here
          Next
       End If
    Next
 Next

Because ContextMenu appears when I click on label?
Pete O'Hanlon 12-Feb-18 8:20am    
If I were doing this (sorry, but this is C#), it would look something like this:
private void IterateOverControl(ControlCollection controls)
{
  if (controls == null) return;
  foreach (Control control in controls)
  {
    IterateOverControl(control.Controls);
    if (control.ContextMenu != null)
    {
      // Do something here because I know I have a context menu.
    }
  }
}
You can then call this by:
IterateOverControls(this.Controls);
It should be a simple matter for you to translate that to VB.NET.
Simos Sigma 12-Feb-18 9:42am    
I converted it to VB.NET but I can't make it work... I am trying several things now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900