1,105,402 Community Members

Creating Form Menus by Code

Member Avatar
Bile
Junior Poster in Training
96 posts since Dec 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

-->>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?
-->>Thanx.

Member Avatar
jhai_salvador
Posting Whiz in Training
237 posts since Mar 2008
Reputation Points: 48 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 2 [?]
Sponsor
 
0
 

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
    End
    Begin VB.Menu submnuGroups
    Caption = "Edit Group"
    Index = 2
    End
    Begin VB.Menu submnuGroups
    Caption = "Delete Group"
    Index = 3
    End
    End

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
      End
      Begin VB.Menu submnuGroups 
         Caption         =   "Edit Group"
         Index           =   2
      End
      Begin VB.Menu submnuGroups 
         Caption         =   "Delete Group"
         Index           =   3
      End
   End
End
Attribute VB_Name = "frmMain"

Thats it.

Member Avatar
RonalBertogi
Junior Poster in Training
64 posts since Nov 2012
Reputation Points: 46 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
RonalBertogi
Junior Poster in Training
64 posts since Nov 2012
Reputation Points: 46 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Bile
Junior Poster in Training
96 posts since Dec 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
donard.marollano
Newbie Poster
1 post since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Sir how about save button codes?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

@Donard,

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?

Member Avatar
Bile
Junior Poster in Training
96 posts since Dec 2010
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

-->>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.

Member Avatar
sachins1986
Newbie Poster
5 posts since Nov 2012
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

please upload the project of menu using coding

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Only a pleasure Bile. :) You know what to do now... please mark as solved, thanx.

Question Answered as of 1 Year Ago by AndreRet, RonalBertogi, jhai_salvador and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article