Title | Change a popup menu's items at run time |
Keywords | popup, dynamic popup, context menu, dynamic context menu |
Categories | Controls |
|
|
Make a popup menu item with Index = 0. At run time, use Load to create new items in the control array. Use Unload to remove them. This example also uses the control array's UBound property to get the largest index of the items in the array.
This routine adds a new item to the menu:
|
|
Private Sub cmdAddItem_Click()
Dim idx As Integer
' Get the next index number.
idx = mnuPopupItem.UBound + 1
' Load the control.
Load mnuPopupItem(idx)
mnuPopupItem(idx).Caption = "Item " & Format$(idx)
cmdRemoveItem.Enabled = True
End Sub
|
|
This routine removes an item from the menu.
|
|
Private Sub cmdRemoveItem_Click()
Dim idx As Integer
' Get the last index number.
idx = mnuPopupItem.UBound
' Unload the control.
Unload mnuPopupItem(idx)
cmdRemoveItem.Enabled = (idx > 1)
End Sub
|
|
You cannot use Unload to unload the menu item with Index = 0 that you created at design time. If you want to be able to remove all of the items from the menu, set this one's Visible property to False to hide it.
|
|
|
|