-->>Hi,I am getting tired of recreating the menus in every form I design my be due to lack of information...
-->>Thus I was wondering if there is a way that I write codes in may be a module so that I may be using it...
-->>by just calling it in my form?

4 Years
Discussion Span
Last Post by AndreRet

heres what you can do:

  1. Open your template form in notepad (form with the extension of .frm) - this is the form you want to copy the menu.
  2. After you open it with notepad, find the Begin VB.Menu and COPY it. Something like this;

    Begin VB.Menu mnuGroups
    Caption = "&Groups"
    Begin VB.Menu submnuGroups
    Caption = "Add Group"
    Index = 1
    Begin VB.Menu submnuGroups
    Caption = "Edit Group"
    Index = 2
    Begin VB.Menu submnuGroups
    Caption = "Delete Group"
    Index = 3

Make sure you copy from Begin VB.Menu up to the End correctly as you can see on the snipped above.

  1. Open the other form in notepad - this is the form where you want to recreate the menu.
  2. Find the Attribute VB_Name and above it, there should be End, now, paste your copied code above that End

it will look something like this after you paste it in the form where you want to recreate the menu;

   Begin VB.Menu mnuGroups 
      Caption         =   "&Groups"
      Begin VB.Menu submnuGroups 
         Caption         =   "Add Group"
         Index           =   1
      Begin VB.Menu submnuGroups 
         Caption         =   "Edit Group"
         Index           =   2
      Begin VB.Menu submnuGroups 
         Caption         =   "Delete Group"
         Index           =   3
Attribute VB_Name = "frmMain"

Thats it.

Edited by jhai_salvador


Create your form with all the menus you need, save it (with the name that will help you identify it's contents) and copy it to the forms templates folder of visual basic. About this folder, :( I can no longer remember where exactly it is. Haven't been using vb6 for quite a long time now.

Edited by RonalBertogi


Oh, Bile, I guess it's in Program Files\Microsoft Visual Studio\VB98\Template. In that folder there are different subfolders that for each respective type of template: forms, classes, etc. etc.


-->>Owkay jhai_salvador and RonalBertogi Let me Check on it then I'll Keep You Posted...
-->>As I should try both Methods...


You can create menu items at run time...

Design Time

First right click any place on your form where there are no controls.
Then choose Menu Editor…
Now in the editor add a main menu,
Give it a caption such as Favorites and a name such as mnuFav.
Then click next,
Then the right arrow button so that the new menu will appear under it.
Now give this menu a fake caption for now like My Caption and give it a name such as mnuFavLinks.
Finally set its index property to 0. By setting its index property, you tell VB that you want this menu to be the first element in the control array.
That's all you have to do at design time.

Run Time

Now, at runtime to add a new menu to the menu control array you simply use the command: Load mnuFavsLinks(index) where index is the next number in the control array. For example I want three favorites under my Favorites menu. I want them to show up as Favorite 1, Favorite 2, and Favorite 3.

In a module...

Public Sub LoadMenus()
    mnuFavLinks(0).caption = "Favorite 1" 
   Load mnuFavLinks(1) 
   Load mnuFavLinks(2) 
   mnuFavLinks(1).Caption = "Favorite 2" 
   mnuFavLinks(2).Caption = "Favorite 3"
End Sub

This is the code I would use in the form load event:

Private Sub Form_Load() 
   Call LoadMenus
End Sub

Handling Clicks

Incase you don't know how control arrays work; all three of my favorites above will have the same event procedure associated with them. So in my example above the mnuFavLinks_Click event would fire for all three Favorites, and the only way to tell them apart is to check their index, which gets passed into the event procedure. The example below will show you what I mean. All it does is when you click on one of the favorites, a message box will appear telling you its index and its caption.

Private Sub mnuFavLinks_Click(Index As Integer)
   MsgBox Index & ": " & mnuFavLinks(Index).Caption
End Sub 

There you have it. Now your Visual Basic programs can add menu's at runtime whenever your want.

Edited by AndreRet



This thread belongs to Bile. Please open your own post and we will answer there, thanx. You will also need to be much more specific about your question, what about button codes?


-->>That is xo satisfactory to Me Andre!!! I wanted somthing lyk that...having a module where all my menus are defined there and just call them in any form I want.
-->>Now its My turn to play with Control array thing I hope It wont be xo difficult as I'm a little familiar with and as well your Example above seems to be more Useful to Me as it has everything I want there...
-->>Thank you all.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.