method Menu.Add (Caption as String, [ItemType as ItemTypeEnum], [ID as Variant])

Adds a new item to menu and retrieves the newly created object.

   Caption as String A string expression that indicates the item's caption or the file path if the ItemType is ShortCut.  
   ItemType as ItemTypeEnum An ItemTypeEnum expression that indicates the item's type.  
   ID as Variant A long expression that indicates the item's identifier.  
  ItemAn Item object that represents the newly created item. 

The Caption parameter supports built-in HTML like described in the Caption property. Use the Add property to add items to the menu. Use Clear method to clear your popup menu. Use SubMenu property to access the item's sub menu. Add method adds an icon to Images collection that corespond to file path, if the newly item created is of ShortCut type.. Use the Remove method to remove an item. Use the Visible property to show or hide an item.

The following sample shows how to add an item:

PopupMenu1.Items.Add "Item", Default, 1234

The following sample shows how to add a new separator item:

PopupMenu1.Items.Add "", Separator

The following sample shows how to add item to a submenu:

Set m = PopupMenu1.Items.Add("SubMenu", SubMenu).SubMenu
m.Add "Item 1", Default, 1
m.Add "Item 2", Default, 2
m.Add "Item 3", Default, 3

The following sample shows how to add an item of ShortCut type to your menu:

Dim i As Item
' Adds a new item of SubMenu type that points to a Folder.
Set i = .Add("C:\Documents and Settings\All Users\Start Menu\Programs", ShortCut)
' Adds shorcuts to files.
i.SubMenu.Add("C:\Program Files\Internet Explorer\IEXPLORE.EXE", ShortCut, 1234).Caption = "Internet Explorer"
i.SubMenu.Add("C:\WINNT\explorer.exe", ShortCut, 1235).Caption = "Windows Explorer"
.Add "c:\winnt\system32\Notepad.exe", ShortCut, 1236
' Adds a shorcut to a folder. The newly created item contains no submenu, and needs an identifier
.Add "C:\Documents and Settings\All Users\Start Menu\Programs", ShortCutFolder, 1237

The following sample shows how to add your Notepad application to menu:

PopupMenu1.Add "c:\winnt\system32\Notepad.exe", ShortCut, 1236

The following sample shows how to run the application when a item of ShortCut type is selected:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOW = 5
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As Long
    i = PopupMenu1.ShowAtCursor
    If (i >= 1234) Then
        ' Is Internet Explorer?
        If i = 1234 Then
            ' You can call ShellExecute API and you can pass argumets ( in this case we pass "" for Internet Explorer item!
            ShellExecute 0, "open", PopupMenu1.Command(i).FilePath, "", "", SW_SHOW
            ' You can call ShellExecute API and you can pass argumets!
            ShellExecute 0, "open", PopupMenu1.Command(i).FilePath, "", "", SW_SHOW
        End If
    End If
End Sub

Send comments on this topic.
1999-2016 Exontrol.COM, Software. All rights reserved.