Title | Dynamically create and remove menu items at run time |
Keywords | menu, dynamic menu |
Categories | Controls |
|
|
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
|
|
|
|