We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,624 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Creating Form Menus by Code

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

6
Contributors
10
Replies
4 Days
Discussion Span
5 Months Ago
Last Updated
11
Views
Question
Answered
Bile
Junior Poster in Training
90 posts since Dec 2010
Reputation Points: 20
Solved Threads: 3
Skill Endorsements: 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.

jhai_salvador
Posting Whiz in Training
226 posts since Mar 2008
Reputation Points: 58
Solved Threads: 39
Skill Endorsements: 2

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.

RonalBertogi
Light Poster
42 posts since Nov 2012
Reputation Points: 26
Solved Threads: 10
Skill Endorsements: 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.

RonalBertogi
Light Poster
42 posts since Nov 2012
Reputation Points: 26
Solved Threads: 10
Skill Endorsements: 0

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

Bile
Junior Poster in Training
90 posts since Dec 2010
Reputation Points: 20
Solved Threads: 3
Skill Endorsements: 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.

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

Sir how about save button codes?

donard.marollano
Newbie Poster
1 post since Nov 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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?

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

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

Bile
Junior Poster in Training
90 posts since Dec 2010
Reputation Points: 20
Solved Threads: 3
Skill Endorsements: 0

please upload the project of menu using coding

sachins1986
Newbie Poster
5 posts since Nov 2012
Reputation Points: 12
Solved Threads: 1
Skill Endorsements: 0

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

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20
Question Answered as of 5 Months Ago by AndreRet, RonalBertogi, jhai_salvador and 2 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0880 seconds using 2.67MB