Working with menus in Visual Basic
Menus in Visual basic 6.0 are created using the Menu object defined in the VB Library. The menu created so are flat Win98 style menus and do not support icons. Further they cannot be created at runtime and require "Menu Editor", modification though can be made during runtime.

Click on "Menu Editor..." under Tools on main menubar. Menu editor is accessible only if a form is focussed.

Caption field is the string that is visible on the Menu. Adding an Ampersand (&) before a character makes that character the 'Hot Key".
Name field is the name for the menu item and should be unique for the form. An object with this name is visible in the Code section of VB alongwith a single event "Click".
Index field is useful if you want to create an array of menu items, in case of which more than one menu items will have the same Name with different Index value. This way the click event for the menu item will also recieve the Index value as parameter and actions will be performed accordingly.
Shortcut dropdown list allows you to specify a shortcut that will fire the event associated with the particular Menu item.
HelpContextID is useful if your Application help file handles Context sensitive help requests.
By default if a linked object or an embedded object is active the top level menus and their submenus are displayed on the toolbar. Setting the Negotiate position field determines the position i.e. (Left, right or middle) where the menu would be shown in this case (defualt - Not shown).
Check field determines if a check mark is displayed before the menu item or not. Enabled and Visible fields determine if the menu item is Enabled or not and that it is visible or not. All these fields can be manipulated at runtime.
WindowList field is useful on the MDIForm. If this field is checked, list of all the active MDI clild forms are shown as submenu under the item.
Pressing the right arrow button makes the menu item a submenu under the menu item that's above it in the list. Pressing the Left arrow key negates the effect. Pressing Up and down button moves the selected menu item up/down in the list.
Using the Menu Editor as described above you can create the Menu for use with your application. Now we will discuss three other important topics associated with Menus.
Creating Menu Seperators
The Menu seperators are implemented in VB6 by creating dummy Menu items with single hyphen "-" set as caption.
As required with all menu items these too need to have unique names and though you can never click them, the objects are visible in code window with the click events. To prevent unnecessary listings in the code window it is preferable that all seperators are created as array of a single menu item.
Creating Context Sensitive Menu
Context Sensitive Menus are around us since Windows 95 where right clicking on an object shows up a menu related to the object. This can be implemented in VB using the PopMenu method of the form object.
PopupMenu Menu As object, [Flags], [X], [Y], [DefaultMenu]
The first parameter is the menu, submenu of which is to be displayed as context sensitive menu. That is, whatever menu Items you need to display as context sensitive menu should be placed under a menu item which is passed to the PopupMenu method as parameter. This menu item can be invisible if not required elsewhere in application.
The second parameter, optional, is the flag which is one or combination of more of the flags mentioned below. First two applies by default.
Const vbPopupMenuLeftAlign = 0 ' Menu is aligned Left to the point where mouse was clicked Const vbPopupMenuLeftButton = 0 ' Menu disappears if the Left mouse button is clicked anywhere outside the menu area Const vbPopupMenuRightButton = 2 ' Menu disappears if the Right mouse button is clicked anywhere outside the menu area Const vbPopupMenuCenterAlign = 4 ' Menu is aligned centrally about the point where mouse was clicked Const vbPopupMenuRightAlign = 8 ' Menu is aligned Right to the point where mouse was clicked
The Third and Fourth parameters are the co-ordinates where menu is placed, this can be used if you want to manually specify the menu location.
The last parameter is the menu item which is to be marked as default by making the item bold.
Creating Dynamic Menu
Since the menus cannot be created during runtime, complete dynamic control over the menu is not available. However since VB6 allows Array of menu items using index property, this feature can be exploited to create some dynamism.
Say you want to make available the list of files recently accessed as menu items under "File" menu item, similar to what is available with MS Office Application and many others. The number of files can be anything say 10. How to do that?
Using the "Menu Editor" create a menu item say mnuFiles giving index value as 0. This will now treat the menuitem as array. Make with menu item hidden by unchecking it's visible property.
Now say your filenames are stored as a collection
Dim objFilenames As New Collection objFilenames.Add "c:\filename1" objFilenames.Add "c:\filename2" objFilenames.Add "c:\filename3" objFilenames.Add "c:\filename4"
To dynamically create a menu item for each of these file simply require to code as below
For ctr = 1 To objFilenames.Count
Load mnuFiles(ctr)
mnuFiles(ctr).Caption = objFilenames.Item(ctr)
mnuFiles(ctr).Visible = True
Next
Here for each filename in the collection you have loaded a new menu item with a new index value, change the caption to show the file name and make it visible.
Similarly you can, if needed, can also add menu items to the top level. Unfortunately a dynamically created menu item cannot be programmed this way to contain submenus.
The click event for mnuFiles will now get Index as parameter which will help you perform desired action. I have simply shown a msgbox giving the file name.
Private Sub mnuFiles_Click(Index As Integer)
MsgBox objFilenames.Item(Index)
End Sub




April 16th, 2007 - 12:17
Hi!!!
I don’t know whether its tooo much of me to ask for!!!! its just a suggestion..Lot of people talk about Role Based Security ofcourse its a Buzz word in .net, How about presenting Role Based Acess using Visual Basic 6.0 menus??? (using database) or which ever you think is the best???!!!
April 16th, 2007 - 12:45
That’s no where near too much!… thanks for going through all posts and for the queries…
Yes Role based Access to menus are indeed needed, specially for something like financial applications etc.
If we take the VB6 way, the same can be applied by using what I described under “Creating Dynamic Menu”… Though the example showed how you can implement the dynamic menus for creating a file list, it can also be used for creating dynamic submenus based on whether a user is eligible for accessing or not… All clicks would be handled by a single click event of menu and which would call different functions using select/case…
A post dedicated entirely to creating menus using WinAPI functions is long due, I hope to take it within this month.
April 17th, 2007 - 04:36
Hello!!
Very soon you may have to launch your own Website is Guess…coz.. this spot is toooooooooo HOT !!!!
I got this idea after going through your article !!! on Menus…especially the section where you are describing “Creating Dynamic Menu”
Great Job!!! Very Kind of you…
regards
April 17th, 2007 - 06:39
Thanks Phil for the compliments!
Hope the information contained could give an idea for your query on how to implement role based menus. I will take full post later sometime
April 17th, 2007 - 08:49
Well just for your Info… I have pasted a IDEA did u go through that???
April 17th, 2007 - 09:45
Hi Phil,
Yes and that’s available at Changing UI Language during Runtime.
I have not taken the Right-to-Left presentation of Arabic text which I feel should happen automatically (Just Check)… In case it doesn’t also carry additional codes as change RightToLeft property of all Labels to True, and TextAlign properties of other controls to fmTextAlignRight.
Do revert with status. I am waiting for it…
October 23rd, 2007 - 21:49
Hello
How to raise an event on the programmaticaly created menus
Liju
November 14th, 2007 - 14:31
Thank you! Thank you! I was searching all over for the _Click event code, and your explanation finally had it.
November 15th, 2007 - 03:56
@Liju
See the last piece of code… the click event for the menu is raised by passing the index. Remember all we are doing is increasing the elements in a array… we never created a menu from scratch programatically.
@Linda
I am glad that you were benefited by the post. Thanks for visiting and appreciating.
April 21st, 2008 - 05:44
Hi..
Any one tell me how can i make the text in menu make to be bold and how can i allign it.It is so urgent.
September 25th, 2008 - 06:11
very nice topic
hie all
i m having a menu array with an array of dates from a database fields
i was wondering if anybody could tlell me how to show the caption of selected item from menu array in a message box or say textbox :s
February 8th, 2009 - 09:34
hi!!
thank you for your site
it definitely help me alot..
May 16th, 2009 - 14:25
Hi I have menu structure in VB using Menu Editor Like
File
|
|—File1
| |
| |— File2
| |
| |— File3
|
|—File2
How Can I Get Top Menu Name For Each Sub Menu Name ?
I’m using to get all menu name
with the following Code
dim MnCtl As Control
For Each MnuCtl In Me.Controls
if type of MnuCtl Is Menu Then
mnuCtl.Name
endif
next
Pls Help And Give Good Sugges. Thank You Reply ASAP, Urgent Need.
June 20th, 2009 - 19:43
This is quite a hot information. I think I’ll share it on Delicious.
p.s. Year One is already on the Internet and you can watch it for free.
December 11th, 2009 - 01:52
you are wrong. vb can create menus during time.
July 7th, 2009 - 03:19
hi, actually i don’t know what is how to show the caption of selected item from menu array in a message box because our teacher hasn’t taught us anything