Creating A Menu in Visual Basic

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

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:
2. For "Caption", 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).

For "Name", type mnuFile.

Your Menu Editor screen should look like this:


Click the Next button.
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).

1. Start a new VB project and place a label on the form. Name the label lblTestText. Set the
Caption to Test Text.

2. Open the Menu Editor, and create a top-level item with a Caption value of PopUpFormat and
the Name mnuPopuUpFormat. Also importantly uncheck the Visible checkbox (see the
circled item below). In order for a menu to be a pop-up menu, it must be invisible.

3. Create the following level-two menu items below the PopUpFormat top-level menu. (When
creating these level-two items, keep the Visible box checked.)

Caption Name
Bold mnuBold
Italic mnuItalic
Underline mnuUnderline
- (hyphen) mnuFormatSep
Cancel mnuCancel

When done, your Menu Editor should look like this:


4. Click OK to save your changes. Note: When you return to the IDE, you will NOT see this menu
on the form (remember it's a pop-up menu, and it will only be visible when invoked
through code).

5. Code the lblTestText_MouseDown event as shown below. Note that the Button parameter is
tested for vbRightButton as is conventional, we only want to pop up the menu if the user
right-clicks on the label. If the user clicks the right mouse button, the PopupMenu
statement is executed. It is this statement that makes the pop-up menu appear.

Private Sub lblTestText_MouseDown(Button As Integer, _


Shift As Integer, _

X As Single, _

Y As Single)

If Button = vbRightButton Then

PopupMenu mnuPopUpFormat, vbPopupMenuRightButton

End If

End Su

The full syntax for the PopupMenu method, from MSDN, is:

object.PopupMenu menuname, flags, x, y, boldcommand

The PopupMenu method syntax has these parts:


Part Description
object Optional. An object expression that evaluates to an object in the Applies
To list. If object is omitted, the form with the focus is assumed to be
object.
Menuname Required. The name of the pop-up menu to be displayed. The specified
menu must have at least one submenu.
Flags Optional. A value or constant that specifies the location and behavior of
a pop-up menu, described as follows:

Constant (location) Value Description


vbPopupMenuLeftAlign 0 (Default) The left side of
the pop-up menu is
located at x.
vbPopupMenuCenterAlign 4 The pop-up menu is
centered at x.
vbPopupMenuRightAlign 8 The right side of the pop-
up menu is located at x.

Constant (behavior) Value Description


vbPopupMenuLeftButton 0 (Default) An item on the
pop-up menu reacts to a
mouse click only when
you use the left mouse
button.
vbPopupMenuRightButton 2 An item on the pop-up
menu reacts to a mouse
click when you use either
the right or the left mouse
button.

Note: To specify both a "location" constant and a "behavior" constant,


add the two values together. For example:

PopupMenu MyMenu, vbPopupMenuRightAlign +


vbPopupMenuRightButton

X Optional. Specifies the x-coordinate where the pop-up menu is


displayed. If omitted, the mouse coordinate is used.
Y Optional. Specifies the y-coordinate where the pop-up menu is
displayed. If omitted, the mouse coordinate is used.
boldcommand Optional. Specifies the name of a menu control in the pop-up menu to
display its caption in bold text. If omitted, no controls in the pop-up
menu appear in bold.

5. Code the mnuBold_Click event as shown below. Note that the Checked property of the menu
item is used. When set to True, this causes a checkmark to appear to the left of the menu
item. The Checked property is typically used as a toggle.

Private Sub mnuBold_Click()

If mnuBold.Checked Then

lblTestText.FontBold = False

mnuBold.Checked = False

Else

lblTestText.FontBold = True

mnuBold.Checked = True

End If

End Sub

6. Code the mnuItalic_Click and mnuUnderline_Click events in a similar fashion as shown


below.
Private Sub mnuItalic_Click()

If mnuItalic.Checked Then

lblTestText.FontItalic = False

mnuItalic.Checked = False

Else

lblTestText.FontItalic = True

mnuItalic.Checked = True

End If

End Sub

Private Sub mnuUnderline_Click()

If mnuUnderline.Checked Then

lblTestText.FontUnderline = False

mnuUnderline.Checked = False

Else

lblTestText.FontUnderline = True

mnuUnderline.Checked = True

End If

End Sub

7. Run the program and check out the various options you have coded.

8. Save the program and exit VB.


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. 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. 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, but can be
used as a pop-up menu invoked with the PopupMenu method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy