Question

I need to do something with a ContextMenuStrip if it exists on a form.

Therefore I tried

    For Each c As Control In uForm.Controls
        If TypeOf c Is ContextMenuStrip Then

            Dim n As ContextMenuStrip
            n = DirectCast(c, ContextMenuStrip)

            For Each c2 As ToolStripMenuItem In n.Items
                MsgBox(c2.Text)
            Next
        End If
    Next

However, the ContextMenuStrip is not "there" when I iterate over the controls. For example, when I put a button on a form, then this button is found using the above iteration. But the ContextMenuStrip isn't.

Thank you for the help!

Was it helpful?

Solution

Try Like This

For Each c As Control In uForm.components.Components

        If TypeOf c Is ContextMenuStrip Then

            Dim n As ContextMenuStrip
            n = DirectCast(c, ContextMenuStrip)

            For Each c2 As ToolStripMenuItem In n.Items
                MsgBox(c2.Text)
            Next
        End If

    Next

I tested Like this : This is you want?

   For Each c As Control In Me.components.Components

            If TypeOf c Is ContextMenuStrip Then
                MsgBox("sadfsadfsd")
            End If

        Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top