Tuesday, July 15, 2014

Creating nth level menu by recursion


Private Sub AppendChildToMenuStrip(ByRef DropDownItems As ToolStripItemCollection, _
                                       ByVal Tag As String, _
                                       ByVal Title As String, _
                                       ByVal ParentIDAs Integer)

            For Each Item As Object In DropDownItems

                Dim SubMenu As ToolStripMenuItem = TryCast(Item, ToolStripMenuItem)

                If SubMenu.Tag = ParentID Then

                    Dim ToolStripItem As New ToolStripMenuItem

                    ToolStripItem.Tag = Tag : ToolStripItem.Text = Title

                    SubMenu.DropDownItems.Add(ToolStripItem)

                ElseIf SubMenu.HasDropDownItems Then

                    AppendChildToMenuStrip(SubMenu.DropDownItems, Tag, Title, ParentID)

                End If

            Next

    End Sub

No comments:

Post a Comment