Menu Strip Newbie Question

batutabunsing

Member
Joined
Mar 3, 2008
Messages
19
Programming Experience
Beginner
Hello everyeone.

I am a newbie with regards to development and programming and I have chosen the path of Winforms and VB.Net.

Currently, I am developing an application. This application has a main form, where there is also a menu item. The menu item corresponds to opening the different windows.

I think they call this development MDI..

Anyway, I got a few questions which I think most of you would be able to answer right away.

In my main form, I have a menu setup like this:
Requests
-- Car
-- Service
Schedule
-- Daily
-- Monthly
Reports
-- Transport
-- Travel
-- Allowances

Now, When I click on menu item for example Requests | Service, i am supposed to open a win form named frmService.

I double click on the menu item and the following code comes out.
VB.NET:
Private Sub menu1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menu1.Click
        

End Sub

As you can see, I want to know which menu item I have clicked so that i could open the appropriate window.

Like for example:

VB.NET:
Private Sub menu1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menu1.Click
       ' Requests | Service item clicked
	   frmService.Show()
	   
	   ' Schedule | Daily item clicked
	   frmDailySchedule.Show
 End Sub



Next question I got is this..

Since we know that the parent form contains the menu above, I have a different set menu for my other winforms.

Like for example in my frmServices, there will be a menu whose structure is this
Machine
-- Computer
-- Fax
Office Equipment
-- Tables
-- Chairs


So that when the frmService window is opened, the menu which is displayed on the main parent will become the menu of the child window

I hope you guys and gals could give me advice.

Thank you.
 
Each menu item has its own Click event, what you got there with your strangely named "Menu1" Click is only the "Requests | Service item clicked". Dbl.click another menu item to get an event handler for that too.

You can also handle the events of all menu items with one handler method, then you would cast the "sender" to a menu item data type and for example check it's Text like this: CType(sender, ToolStripMenuItem).Text

Another option if you want a handler for all sub items of a item like "Requests" you select this control and handle its DropDownItemClicked, where you get the sub item from event parameter e.ClickedItem.
 
Hi JohnH. Thanks for the reply. You were right when you mentioned that I only clicked the topmost menu item.. When I double clicked the menu sub menu item, an event was presented to me where I was able to place the necessary code.

With regards to my other question, I am trying to develop a MDI application. The main window, has a set of menus. Now the child windows also have their own set of menus. When I open the child windows, their menu items get joined together. Is it possible that only the menu item of the child window be present in the parent (main form) when the child window is open?

Thanks a lot. Learning winforms is quite a challenge.
 
Each menu item has its own Click event, what you got there with your strangely named "Menu1" Click is only the "Requests | Service item clicked". Dbl.click another menu item to get an event handler for that too.

You can also handle the events of all menu items with one handler method, then you would cast the "sender" to a menu item data type and for example check it's Text like this: CType(sender, ToolStripMenuItem).Text

You also mentioned here that I can just have one event handler for all the menu items..

Sort of like this..

Sub Menu1_Clicked
Dim strItem as String = Ctype(sender, ToolStripMenuItem).Text

Select Case strItem
Case "Menu1"
'Do process for menu1
Case "Menu2"
'Do process for menu2
......

End Select

End Sub

How do i wire up all the events if for example when I double click on the sub menu item, an event handler is created for it?

Thanks a lot.
 
You use the handles part:
VB.NET:
Private Sub Menu1_Click (...) Handles Menu1.Click, Menu2.Click, Menu3.Click, etc...
 
You use the handles part:
VB.NET:
Private Sub Menu1_Click (...) Handles Menu1.Click, Menu2.Click, Menu3.Click, etc...

Ok thanks a lot for this. I know I got a lot more to learn with VB.Net and WinForms programming.

On my other question, how do i do the menu of the child, become the menu of the main mdi frame?

Say..

formParent
Menu Items
File
-- New
-- Open
Edit
-- Cut
-- Copy
Help
-- About

formChild
Menu Items
Modify
-- Data
-- Requests
Reports
-- Data
-- Requests

When I invoke to open the formChild, the menu displayed in the formParent should be the one which is from formChild. Currently, what happens is that the menu becomes mixed.

Thanks.
 
Back
Top