Working with Menus in VB6

Level:
Level2

This tutorial demonstrates how to build menus in VB programs.

 

EXAMPLE 1:

 

The end result of Example 1 will be a form containing a menu with two top-level items, File and Help.

 

The File menu, shown below, will have the following level-two items below it: New, Open, Save, Save As, Print, and Exit. Note that separator bars appear above the Save, Print, and Exit items.

 

 

The Help menu contains just one level-two item below it, About.

 

 

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.

 

3. Click the "right-arrow" button (shown circled below). A ellipsis (...) will appear as the next item in the menu list, indicating that this item is a level-two item (below "File").

 

 

For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N. By specifying a shortcut, you allow the user to access the associated menu item by pressing that key combination. So here, you are providing the user three ways of invoking the "New" function: (1) clicking File, then clicking New on the menu; (2) keying Alt+F,N (because we set up an access key for "N" by placing an ampersand to left of "N" in "New"); or (3) keying Ctrl+N. At this point, your Menu Editor screen should look like this:

 

 

Click the Next button.

 

4. For "Caption", type &Open; for "Name", type mnuOpen, and for "Shortcut", select Ctrl+O. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

5. For "Caption", type - (a hyphen), and for "Name", type mnuFileBar1. A single hyphen as the Caption for a menu item tells VB to create a separator bar at that location. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

6. For "Caption", type &Save; for "Name", type mnuSave, and for "Shortcut", select Ctrl+S. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

7. For "Caption", type Save &As ..., and for "Name", type mnuSaveAs. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

8. For "Caption", type -, and for "Name", type mnuFileBar2. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

9. For "Caption", type &Print;for "Name", type mnuPrint; and for "Shortcut", select Ctrl+P. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

10. For "Caption", type -; and for "Name", type mnuFileBar3. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

11. For "Caption", type E&xit, and for "Name", type mnuExit. Your Menu Editor screen should look like this:

 

 

Click the Next button.

 

12. Click the "left-arrow" button (shown circled below). The ellipsis (...) no longer appears, meaning we are back to the top-level items.

 

 

For "Caption", type &Help; and for "Name", type mnuHelp. Your Menu Editor screen should look like this:

 

 

 

Click the Next button.

 

13. Click the "right-arrow" button to create a level-two item below "Help". For "Caption", type &About; and for "Name", type mnuAbout. Your Menu Editor screen should look like this:

 

 

14. At this point, we are done creating our menu entries, so click the OK button. That will dismiss the menu editor and return focus to the VB IDE.

 

 

15. Back in the VB IDE, your form will now have a menu, based on what you have set up in the Menu Editor. If you click on a top-level menu item (File for example), the level-two menu will drop down:

 

 

 

16. Click on the New menu item. The code window for the mnuFileNew_Click event opens, as shown below. Note: Click is the only event that a menu item can respond to.

 

 

In thePlace mnuFileNew_Click event, place the code you want to execute when the user clicks the New menu item. Since this is just a demo, we will place a simple MsgBox statement in the event procedure:

 

MsgBox "Code for 'New' goes here.", vbInformation, "Menu Demo"

 

 

17. Code similar MsgBox statements for the Open, Save, Save As, and Print menu items:

 

Private Sub mnuFileOpen_Click()

 

MsgBox "Code for 'Open' goes here.", vbInformation, "Menu Demo"

End Sub

 

 

Private Sub mnuFileSave_Click()

 

MsgBox "Code for 'Save' goes here.", vbInformation, "Menu Demo"

 

End Sub

 

 

Private Sub mnuFileSaveAs_Click()

 

MsgBox "Code for 'Save As' goes here.", vbInformation, "Menu Demo"

 

End Sub

 

 

Private Sub mnuFilePrint_Click()

 

MsgBox "Code for 'Print' goes here.", vbInformation, "Menu Demo"

 

End Sub

 

18. For the Exit menu item Click event, code the statement Unload Me.

 

Private Sub mnuFileExit_Click()

 

Unload Me

 

End Sub

 

19. For the About menu item Click event, code as shown below:

 

Private Sub mnuHelpAbout_Click()

 

MsgBox "Menu Demo" & vbCrLf _

& "Copyright " & Chr$(169) & " 2004 thevbprogrammer.com", , _

"About"

 

End Sub

 

20. Run the program. Note how the code executes when you click on the various menu items. Also test the use of the access keys (e.g., Alt+F, N) and shortcut keys (e.g., Ctrl-O).

 

 

21. Save the program and exit VB.

 

Download the project files for this example here.

 

 

EXAMPLE 2:

 

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 Sub

 

 

 


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.

If you enjoyed this post, subscribe for updates (it's free)

thanks

thanks sooo very much, the tutorial has been helpful in my project

thanq very much :):):) this

thanq very much :):):) this code helped me alot ..thanq once again..:)

hey, nys tutorial bt my

hey, nys tutorial bt my problem is i have created th menu and everythng so wat i want is to disable some menus from being active if a user logs in and is nt meant to use that menu item. i had put ths code from my log in form
if ...then {ok button code...}
main.show
mnuSave.Enabled=False
exit sub
when i run my project th form main.show appears bt nw i get an error message tht "an object is required"
can you help me solve my problem,anyone ????????

VB Script for clicking a EDW GL menu in excel

Hi All,
I have one excel add inn menu in my Excel sheet, menu name is EDW GL. I need VB Script for clicking this menu. I tried to record this, but it is not recording. Please help me.

thankz a lot.........

thankz a lot.........

thank you... this 1 did help

thank you... this 1 did help me a lot

thanks!

thanks!

Can you provide more details?

I need more details to put menu in vb6, i want text to be bold in menu how can do that?

Impressed

Impressed with the code, it helped a lot

Problem with "underscores" in the main menu

Let me explain my problem, which is also a problem saw in the menu created with this tutorial.

Once all the menu in created and Alt+Shortcuts are created for each one of the options using the "&" before the appropiated letter, that letter for each option DOESN'T APPEARS with the underscore under the appropiated letter. Only the text of the option appears, without any letter underscored. Typing Alt+shortcut, it works and from then all the other options in the menu suddently have each one of its letters underscored.

I mean, the undescores for each one of the main option in the menu, initialy appear without the underscore and onlhy when you use for the first time the Alt+ method appear the undescores for all the option in the menu.

In the Menu Demo created in this tutorial, the two main options: "File" and "Help", have their correspondent "&" before the "F" and before the "H" in the definition of the menu, but both underscores (under the "F" and under the "H") do not appear when the menu runs.

Does somesoby know why it happens ????

Thanks very much.

computer

After & (concatination factor) put the "F" of the file in caps lock and the "ile" small in caption.Everything will work completely fine

Problem with underscores

Hello their, really I have the same situation and same problem, and also don't know why !!!. Please if you or some one knows , let me know. Very thanks

its really greate to

its really greate to understand easily................thanks!!!!!

menu editer

it is very good tutorial. but most of mathods are not defined

i Like iit

nice One ! it reLii heLps a LOt .. by the wAy yOu guys Out there,, dO yOu knOw hOw make a menu using FORTRAN 77 Or PASCAL ..?

tnx in advance.. pls dO heLp me :))

array

10 element integers array and print all elemnt value (for each)

nice tutorial

if possible try 2 add search option and implement it too

thank

thank for help????????

Notepad

I recommend anyone to make a replica of notepad after reading this. Thanks.

thank you, ,it help me a lot

thank you, ,it help me a lot and all your tutorial, ,thank you. .God Bless you

thank you, ,it help me a lot

thank you, ,it help me a lot and all your tutorial, ,thank you. .God Bless you

thanks a lot. let me thank u

thanks a lot.
let me thank u in Farsi
خیلی خیلی سپاس گذارم....

Nice code to begin!

Nice code to begin!

Very Good

the wriiten artical is very good for beginer. this artical cover lots of things for the beginer.

thanks,
Deshmukh

very good

thanks a lot. It was useful.

Useless

It is useless without actual menu control coding.

jealous!....if not, make your

jealous!....if not, make your own...

Thanks the way of

Thanks
the way of presentation is good

awesome tutorial...where can

awesome tutorial...where can i get more these kind of tutorial ?

GREAT....

Really great to understand....

Regards,
Dainy
New Delhi, India
9716226007

GREAT....

Really great to understand....

Regards,
Dainy
New Delhi, India
9716226007

it is good wow

it is good wow

thank u so much

u help me alot

sir u draw a its very simple

sir u draw a its very simple good tutorial

hi

I rili like your tutorial it helps a lot..Please help me what will be the codings in a next button when you will get the record in notepad thanks.please help.

its awesome helped me a lot

its awesome
helped me a lot

vb

Its so nice...

thanks...it helps me a

thanks...it helps me a lot...

menu editor

Very easy and user friendly tutorial

man damn fine page

i loved the tutorial

vrrry gud tutorial

i were very glad with page. it help myself do gud programz.
thank yoo

Creating menu in codes...

its cool....

I know that a menu can be created without using the menu editor... using vb codes instead...
Can anyone tell me how its done.. tnx

Thank you very much more

Thank you very much more should be come

thanks

thanks thanks thanks a lot

How to change font size of menu

How to change font size of menu

vb 6.0 problem

hey guys out there. i am preparing an application where i need to make menus. i know i can display menus through menu editor, and also i can open 'open dialogue box' through commondialoguebox. but i don't no how to open a file. plz guide me..

Resolve the problem of murtaza

go here this link:

http://www.vb6.us/tutorials/common-dialogs-vb-tutorial

there's a Open File tutorial using a common dailogs!

i Like!

nice ! :))

Effective Tutorial

This is very much helpful for beginners

query

gdf i have a query about this chapter is that if i want to create the menu in other indian language like Marathi,Kannada then what are the steps to craete this or how i will get this type of menues.

thanks for nice tutorial...

thanks for nice tutorial...

yeah

Thanks for theh tut!
Is there an way to add it to an textbox wihout you'll get the standard context box with : paste,check spelling and so...
thx

Popup Menu with MSHFlexGrid?

Hello All,

Is it possible to use this same code to bind the popup menu with a MSHFlexGrid?? It does not seem to work!!! Is there anything else that i need to specify when trying to invoke the popup menu with a MSHFlexGrid??

Please help me with this

Many thanks for an early reply

Great Tutorial

It was easy to understand and straight to the point.

Many thanks

srinivas

thank u for helping to learn vb

coding

Is ther any where i may find the code to save a file, print and exit in a file menu?

Thks

help!

i need to pass a vb6 metric converter, i don't know how to start and i don't know how to code it. i need that program by next week. can anyone help me? a simple converter will do.

thanx to vb6 us

thank you so much...
you helped me so much....
this site is really perfect for beginners....
may god bless you,,,,,

problem in menu decleration

when i create the menu in the form then an error message displayed which says
Menu control array elements must be contiguous and within the same submenu
plz reply as soon as possible

check the arrangement of

check the arrangement of your menu.

correct:

Menu1
...Menu2
......Menu3

wrong:
Menu1
......Menu2
...Menu3

Hope it helps..

vpaf

thnks its proved useful for

thnks
its proved useful for me for working with pop up menus

Same menu on multiple forms

Hi,

Very good tutorial.
I have a lot of forms and it would be a lot of work to type in all of them all the menu editor instructions needed. How can I put the same menu editor instructions in all forms?

Please help.
Thank you

weew. thank you so much :)

this topic really helped me in my computer project. thank you so much :)

Thanks

dim temp as string
 
temp="www.vb6.us"
 
 
call Thanks(temp)
 
 
 
 
Public Function Thanks(ByVal User as String)
{
 
msgbox "Thank you very much......." + User
 
}

Good tutorial. But how can I

Good tutorial. But how can I create a menu and hide the title bar (BorderStyle = 0) at the same time?

VB

Sir I have working me in VB and advance software make please me email all department data structure.
Thanks
My Name
Shashi Kumar Sharma

VB Menu Editor

This is step by Step very good lesson for dummys! Thank Bydy

A very helpful resource library:

I had never seen like yours'! God may bless all of you!!! Thank you!!!

very nice,but do u describe

very nice,but do u describe how to put icon in the Menubar,??\

how can i use the shortcut

how can i use the shortcut key in 3 commands with same shortcut keys?

how can i use the shortcut

how can i use the shortcut keys in 3 commands with same shortcut keys?

It is not possible

It is not possible

Visual basic

hi what is the code for it to start a new level and also save and save as?

Superb

really very good job because i have done my work

Big Thanks

Thank you...This is a very good tutorial :)

VERY NICE

HI, THIS IS REALLY VERY INFOMATIVE

tutorial s very nice...thanx

tutorial s very nice...thanx

very nice

hi, Thank you very much .
I always forget separate line (how to put) in menus and today I search it in web but there are every thing accept my need :D
have a nice time and life

i am very impressed with

i am very impressed with this tutorial. it is really very good and helpful.
Thanks.

Nice One

Really Very Informative

Thanks

I leave a coment just to say thanks :) Very good tutorial.

this is the good tutorial

this is the good tutorial for the beginner like me.
that is more help full.......

This is excellent

This is really good. actually I cleared all my problems.
Thanks

Reg:purformance

Good Purformance...Your tutorial is very helped for my confussion.

yaa it is also helped

yaa it is also helped clearing my confussion doubt too ... thanks you very much bye take care keep smiling happy program making

Thanks

Thanks my friend, this tutorial really help me a lot..
Thanks for your heartily concern....!

good tutorial

good tut

This Helped me a lot.

This Helped me a lot. Thanks!

This is a very good tutorial

This is a very good tutorial for begginers like me.

Thank you