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.
Related Article: Railway reservation
is a Visual Basic 4 / 5 / 6 discussion thread by mustaffa hasan that has 1 reply and was last updated 11 months ago.
Bile
Junior Poster in Training
90 posts since Dec 2010
Reputation Points: 20
Solved Threads: 3
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
@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
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