MDI Parent and Child Menu

batutabunsing

Member
Joined
Mar 3, 2008
Messages
19
Programming Experience
Beginner
Hi everyone.

I have searched to this question for quite a few days now and with little success in obtaining what I need. I hope someone could help me out here.

Anyway, here's the statement of the problem.

I am developing an MDI application. The Parent form contains some menu items, as well as the child forms.

What I need is for example, If no child window is open, the default menu for the MDI parent form is displayed. If a child form is open, then the menu of the child should be the one displayed in the main form. It should not merge with the menu of the parent. If for example multiple child forms are opened, then the active form (window) menu items should be the one displayed.

Am sure there is way on resolving this and I would love to hear your suggestions.

Thanks.
 
You'd have to actually remove the menu from the child form and add it to the parent form. Will there ever be more than one child form open at a time?

Yes there may be instances when multiple child windows could be open at any given time. It is one system requirement of the business due to comparative data analysis.

I am not sure about removing the menu of the child form since the menu on the child form is actually mapped to some events that performs something particular to the form itself. What I need is for the menu of the frame window (parent) to be removed and the menu of the child window which is open (or active for multiple instances of child windows opened) to be displayed on it.

Thanks.
 
This can actually be solved quite easily by adding another empty MenuStrip to the MdiParent. By selecting this as MainMenuStrip merges for Mdi childs will be done towards this. The empty menustrips Visible property is set False initially. Then you need a little code to swap visibility for the real parent menu and the empty one for when mdi childs are present or not.
VB.NET:
Private Sub MDIParent1_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MdiChildActivate
    If Me.ActiveMdiChild Is Nothing Then
        Me.ParentMenuStrip.Visible = True
        Me.MergeMenuStrip.Visible = False
    Else
        Me.ParentMenuStrip.Visible = False
        Me.MergeMenuStrip.Visible = True
    End If
End Sub
Since child forms menustrips always merge just set Visible to False for them to remove the empty-after-merge strip in child. If you do this you will only see this menu in designer when you select the menu in the components area below the form.
 
This can actually be solved quite easily by adding another empty MenuStrip to the MdiParent. By selecting this as MainMenuStrip merges for Mdi childs will be done towards this. The empty menustrips Visible property is set False initially. Then you need a little code to swap visibility for the real parent menu and the empty one for when mdi childs are present or not.
VB.NET:
Private Sub MDIParent1_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MdiChildActivate
    If Me.ActiveMdiChild Is Nothing Then
        Me.ParentMenuStrip.Visible = True
        Me.MergeMenuStrip.Visible = False
    Else
        Me.ParentMenuStrip.Visible = False
        Me.MergeMenuStrip.Visible = True
    End If
End Sub
Since child forms menustrips always merge just set Visible to False for them to remove the empty-after-merge strip in child. If you do this you will only see this menu in designer when you select the menu in the components area below the form.


Hi JohnH. Thanks for this post.

I am not sure if I do understand you properly (sorry but am a newbie in VB.Net). Anyway, let me tell you how I understand what you meant here.

1. In my Parent form, I need to add another menustrip control. This menu strip would not contain any menu item. So, in my parent form, I have two menustrip:
-- mnuMainMenu > the original menu
-- mnuNothing > the menu which contains nothing

2. I used the code you posted above, just giving the proper names to the menustrip I have used in my application for the frmParent_MdiChildActivate.

Now, when I run the application, initially, the mnuMainMenu is displayed in parent form. When I open a child form, the menu is totally gone, even the menustrip of the child form. How would I be able to show the menu of the child then?

Thanks a lot. Regards.
 
You need to select the "mergemenu" as MainMenuStrip for the mdi parent form. It's a property of the form.
 
You need to select the "mergemenu" as MainMenuStrip for the mdi parent form. It's a property of the form.

Hi JohnH.. Thanks for the reply.

This is my winforms structure..

Parent
-- mnuParent
-- mnuNothing (this is the menu to merge)

Child
-- mnuChild

then the codes..

Private Sub MDIParent1_MdiChildActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MdiChildActivate
If Me.ActiveMdiChild Is Nothing Then
Me.mnuParent.Visible = True
Me.mnuNothing .Visible = False
Me.MainMenuStrip = mnuParent
Else
Me.mnuParent.Visible = False
Me.mnuNothing .Visible = True
Me.Mainmenustrip = mnuNothing
End If
End Sub

But still the child menu does not show in the main menu. Nothing shows on the menu of the Parent form.

I hope you could advise further. thanks.
 
What I did was to set this property in Properties window in designer, which I didn't find any problems with. Your code seems to result in glitches I have not looked further into yet, perhaps it's too late to change it at that stage in the merging process.
 
What I did was to set this property in Properties window in designer, which I didn't find any problems with. Your code seems to result in glitches I have not looked further into yet, perhaps it's too late to change it at that stage in the merging process.

Hi JohnH.

If I set the property for the parent form for the mainmenustrip to the mnuNothing, then no menu item will display.

I need to display some menus in the parent form when i initially opened my application. the parent form's menu items are the ones which calls the child forms.

Would you be able to send out the app you mentioned that works fine on your side. Maybe it would be best for me to see that example so that we can be assured that we're on the same page on this one.

Thanks.
 
Last edited by a moderator:
If I set the property for the parent form for the mainmenustrip to the mnuNothing, then no menu item will display.

I need to display some menus in the parent form when i initially opened my application. the parent form's menu items are the ones which calls the child forms.
With this setup the parent menu only displays when there are no active mdi child forms present. As you asked for, "If no child window is open, the default menu for the MDI parent form is displayed.", and that is exactly what happens when you do as instructed. The instructions in my first post is very accurate as I see it, no more and no less needs to be done. The empty strip is selected as main strip and not visible, the real parent strip is set visible, that's it, the code I posted handles the rest swapping visibility according to mdi child activity.

Selecting the main menu strip does not affect visibility, it's perfectly valid to have multiple menustrips visible at the same time, neither need to be selected as main menu strip as such. Setting the main strip affect which menu child menus will merge to when due.
Would you be able to send out the app you mentioned that works fine on your side. Maybe it would be best for me to see that example so that we can be assured that we're on the same page on this one.
I don't have VS2005 installed. I've attached a project I designed in VS2008 targeted for .Net 2.0, try opening the project file MdiApplication.vbproj.

Is it possible that you at some time during your quest have set the AllowMerge property for a menu to False? This would prevent merging from taking place.
 

Attachments

  • MdiApplication.zip
    16.2 KB · Views: 63
Back
Top