CREATING A MENU IN VISUAL BASIC To build a menu for use with your VB program, you use the Menu

Editor, which appears as an icon in the toolbar of the VB IDE. It is the circled item in the screen shot below:

Alternatively, you can invoke the Menu Editor from the Tools menu item as shown below:

To build the menu described above, perform the following steps.

1. Start a new VB project and invoke the Menu Editor using either method shown above (click the Menu Editor toolbar icon or select the Menu Editor option from the Tools menu). The Menu Editor screen appears, as shown below:

type &File (by placing the ampersand to the left of the "F". we establish "F" as an access key for the File item it enables the user to drop down the File menu by keying "Alt+F" on the keyboard in addition to clicking the "File" item with the mouse).2. For "Caption". Your Menu Editor screen should look like this: . type mnuFile. For "Name".

.Click the Next button.

Name the label lblTestText. Create the following level-two menu items below the PopUpFormat top-level menu. (When creating these level-two items. 1. Set the Caption to Test Text. Start a new VB project and place a label on the form. Also importantly uncheck the Visible checkbox (see the circled item below). it must be invisible.) Caption Bold Italic Underline . your Menu Editor should look like this: .(hyphen) Cancel Name mnuBold mnuItalic mnuUnderline mnuFormatSep mnuCancel When done.CREATING POP UP MENUS IN VISUAL BASIC This example shows you how to create a popup menu (sometimes called a context menu or a right-click menu). Open the Menu Editor. In order for a menu to be a pop-up menu. and create a top-level item with a Caption value of PopUpFormat and the Name mnuPopuUpFormat. 3. keep the Visible box checked. 2.

Note that the Button parameter is tested for vbRightButton as is conventional. It is this statement that makes the pop-up menu appear. Click OK to save your changes. we only want to pop up the menu if the user right-clicks on the label. and it will only be visible when invoked through code). Code the lblTestText_MouseDown event as shown below. 5. If the user clicks the right mouse button.4. Note: When you return to the IDE. you will NOT see this menu on the form (remember it's a pop-up menu. Private Sub lblTestText_MouseDown(Button As Integer. the PopupMenu statement is executed. _ .

The name of the pop-up menu to be displayed. described as follows: Constant (location) vbPopupMenuLeftAlign vbPopupMenuCenterAlign vbPopupMenuRightAlign Value 0 4 8 Description (Default) The left side of the pop-up menu is located at x. A value or constant that specifies the location and behavior of a pop-up menu. An object expression that evaluates to an object in the Applies To list. Constant (behavior) vbPopupMenuLeftButton Value 0 vbPopupMenuRightButton 2 Description (Default) An item on the pop-up menu reacts to a mouse click only when you use the left mouse button. flags. An item on the pop-up menu reacts to a mouse . is: object. x. If object is omitted. Menuname Required. boldcommand The PopupMenu method syntax has these parts: Part Description object Optional. from MSDN. Flags Optional.Shift As Integer. The specified menu must have at least one submenu. y. the form with the focus is assumed to be object. _ Y As Single) If Button = vbRightButton Then PopupMenu mnuPopUpFormat. _ X As Single. The pop-up menu is centered at x.PopupMenu menuname. vbPopupMenuRightButton End If End Su The full syntax for the PopupMenu method. The right side of the popup menu is located at x.

the mouse coordinate is used. If omitted. no controls in the pop-up menu appear in bold. Optional. this causes a checkmark to appear to the left of the menu item. Note that the Checked property of the menu item is used. Note: To specify both a "location" constant and a "behavior" constant. If omitted. Code the mnuItalic_Click and mnuUnderline_Click events in a similar fashion as shown below. Specifies the y-coordinate where the pop-up menu is displayed. The Checked property is typically used as a toggle. 5. Optional. add the two values together.click when you use either the right or the left mouse button. the mouse coordinate is used.Checked = True End If End Sub 6. . When set to True. If omitted. Code the mnuBold_Click event as shown below.FontBold = False mnuBold. Specifies the x-coordinate where the pop-up menu is displayed.FontBold = True mnuBold.Checked Then lblTestText. vbPopupMenuRightAlign + vbPopupMenuRightButton X Y boldcommand Optional. Specifies the name of a menu control in the pop-up menu to display its caption in bold text. Private Sub mnuBold_Click() If mnuBold. For example: PopupMenu MyMenu.Checked = False Else lblTestText.

Run the program and check out the various options you have coded.FontUnderline = False mnuUnderline.Checked Then lblTestText.FontItalic = False mnuItalic. 8.Checked Then lblTestText. Save the program and exit VB.Checked = True End If End Sub 7. .Private Sub mnuItalic_Click() If mnuItalic.FontItalic = True mnuItalic.Checked = False Else lblTestText.Checked = False Else lblTestText.Checked = True End If End Sub Private Sub mnuUnderline_Click() If mnuUnderline.FontUnderline = True mnuUnderline.

Any top-level menu that has its Visible box unchecked in the Menu Editor will NOT appear at the top of the form in the menu bar. Any top-level menu that has its Visible box checked in the Menu Editor will appear at the top of the form in the menu bar you create. but can be used as a pop-up menu invoked with the PopupMenu method.Download the project files for this example here NOTES: If desired. you can have both a "regular" menu and as many pop-up menus as you want on the same form. .