Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleManage menus in an MDI application, approach 1
KeywordsMDI, background, draw, MDIForm, child menus
CategoriesTips and Tricks, Controls
 
When an MDI child form gets the focus, its menus replace those of the MDI form. This can make it hard to coordinate the menus that are shared among child forms and those that are unique to each form.

There are two basic approaches. First, you can put all menus used by any child form on the MDI form and then have the children hide those they do not need. Second, you can give each child its own menus, duplicating those that are shared. The first approach is appropriate when the child forms share many common menus. The second is appropriate when they have little in common.

This example demonstrates the first approach. Click here to see the second approach.

Each child form has two methods ShowMenus and HideMenus. They show/hide the menus used by the form that are not needed when no child form is active. For example, if all child windows and the MDI form all use the same File menu, these routines would not involve that menu.

Note that the child forms all show and hide the mnuFileClose menu item that closes the active form because it doesn't make sense for this menu item to be visible when no child is created.

The following code shows/hides the mnuForm1Menus, mnuFileClose, and mnuFileExitSep menu items for the form Form1.

 
' Show normally hidden menus needed by this form.
Private Sub ShowMenus()
    MDIForm1.mnuForm1Menus.Visible = True
    MDIForm1.mnuFileClose.Visible = True
    MDIForm1.mnuFileExitSep.Visible = True
End Sub

' Hide normally hidden menus needed by this form.
Private Sub HideMenus()
    MDIForm1.mnuForm1Menus.Visible = False
    MDIForm1.mnuFileClose.Visible = False
    MDIForm1.mnuFileExitSep.Visible = False
End Sub
 
A child form invokes these routines whenever it is taking or losing the focus. This happens when the form loads, activates, unloads, or deactivates.
 
Private Sub Form_Activate()
    ShowMenus
End Sub

Private Sub Form_Deactivate()
    HideMenus
End Sub

Private Sub Form_Load()
    ShowMenus
End Sub

Private Sub Form_Unload(Cancel As Integer)
    HideMenus
End Sub
 
That takes care of displaying the menus. Now you need to respond to them. Because they are all on the MDI form, the child forms do not receive menu events directly, the MDI form gets the events instead. The MDI form can forward them from its event handlers to the currently active child form. These routines are specific to the Form1 form:
 
' Call the active form's Black routine.
Private Sub mnuForm1MenusBlack_Click()
    ActiveForm.Black
End Sub

' Call the active form's Red routine.
Private Sub mnuForm1MenusRed_Click()
    ActiveForm.Red
End Sub
 
These routines are handled directly by the MDI form:
 
' Close the active form.
Private Sub mnuFileClose_Click()
    Unload ActiveForm
End Sub

' End the program.
Private Sub mnuFileExit_Click()
    Unload Me
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated