none
Iterating through a menu in VB.NET 2017 RRS feed

  • Question

  • I am using the following to iterate through a main menu: 

    Dim menus As New List(Of ToolStripItem)
    For Each mnu As ToolStripItem In Form1.MenuStrip1.Items
               GetMenus(mnu, menus)
    Next

    Public Sub GetMenus(ByVal Current As ToolStripItem, ByRef menus As List(Of ToolStripItem))
            menus.Add(Current)
            If TypeOf (Current) Is ToolStripItem Then
                For Each menu As ToolStripItem In DirectCast(Current, ToolStripMenuItem).DropDownItems
                    GetMenus(menu, menus)               
                Next
            End If
        End Sub 

    The problem is that I get an error when I run the code:

    System.InvalidCastException occurred
      HResult=0x80004002
      Message=Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripMenuItem'.
      Source=Clientrak
      StackTrace:
       at Clientrak.RoleAssignmentPreference.GetMenus(ToolStripItem Current, List`1& menus) in C:\Users\Dwilson\documents\visual studio 2017\Projects\Clientrak\Clientrak\RoleAssignmentPreference.vb:line 50
       at Clientrak.RoleAssignmentPreference.GetMenus(ToolStripItem Current, List`1& menus) in C:\Users\Dwilson\documents\visual studio 2017\Projects\Clientrak\Clientrak\RoleAssignmentPreference.vb:line 51
       at Clientrak.RoleAssignmentPreference.RoleAssignmentPreference_Load(Object sender, EventArgs e) in C:\Users\Dwilson\documents\visual studio 2017\Projects\Clientrak\Clientrak\RoleAssignmentPreference.vb:line 35
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    How can I test for the ToolStripSeparator?  Am I being too simplistic in my code?

    Thursday, November 16, 2017 1:16 PM

Answers

  • Hi

    Try this and see if it what you want.I only changed one item.

    ' Form1 with MenuStrip1 containing
    ' several menu items, with one separator
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim menus As New List(Of ToolStripItem)
            For Each mnu As ToolStripItem In MenuStrip1.Items
                GetMenus(mnu, menus)
            Next
    
        End Sub
        Public Sub GetMenus(ByVal Current As ToolStripItem, ByRef menus As List(Of ToolStripItem))
            menus.Add(Current)
            If TypeOf (Current) Is ToolStripMenuItem Then
                For Each menu As ToolStripItem In DirectCast(Current, ToolStripMenuItem).DropDownItems
                    GetMenus(menu, menus)
                Next
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 16, 2017 1:47 PM
    • Marked as answer by YandT70 Thursday, November 16, 2017 2:08 PM
    Thursday, November 16, 2017 1:47 PM

All replies

  • Hi

    Try this and see if it what you want.I only changed one item.

    ' Form1 with MenuStrip1 containing
    ' several menu items, with one separator
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim menus As New List(Of ToolStripItem)
            For Each mnu As ToolStripItem In MenuStrip1.Items
                GetMenus(mnu, menus)
            Next
    
        End Sub
        Public Sub GetMenus(ByVal Current As ToolStripItem, ByRef menus As List(Of ToolStripItem))
            menus.Add(Current)
            If TypeOf (Current) Is ToolStripMenuItem Then
                For Each menu As ToolStripItem In DirectCast(Current, ToolStripMenuItem).DropDownItems
                    GetMenus(menu, menus)
                Next
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, November 16, 2017 1:47 PM
    • Marked as answer by YandT70 Thursday, November 16, 2017 2:08 PM
    Thursday, November 16, 2017 1:47 PM
  • Les,

    Thank you!  That got me past the error.  I think I overlook the obvious answer too often.

    I appreciate it.

    David

    Thursday, November 16, 2017 2:10 PM