Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDynamically create and remove menu items at run time
Keywordsmenu, dynamic menu
CategoriesControls
 
Create a menu at design time with Index 0. At run time use the Load statement to create new instances of the menu.
 
Private Sub CmdAdd_Click()
Dim new_index As Integer

    ' Load the new menu item.
    new_index = mnuFileList.UBound + 1
    Load mnuFileList(new_index)
    mnuFileList(new_index).Caption = CaptionText.Text
    mnuFileList(new_index).Visible = True
    CaptionText.Text = ""

    ' Make the separator visible.
    mnuFileSep.Visible = True
End Sub
 
Use Unload to remove menu items.
 
Private Sub CmdRemove_Click()
Dim txt As String
Dim ctl As Menu

    ' Find the menu item with this caption.
    txt = CaptionText.Text
    CaptionText.Text = ""
    For Each ctl In mnuFileList
        If ctl.Caption = txt Then Unload ctl
    Next ctl

    ' Make the separator visible.
    If mnuFileList.Count < 2 Then mnuFileSep.Visible = False
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated