Team LiB
Previous Section Next Section

The Context Menu in MDI Applications

When you write MDI applications, you need to be cognizant of how menus and toolbars are coordinated. If there is a menu for a child window and a menu for the main MDI container form, the two will be merged when the child form opens. The same is true for a toolbar.

But what about pop-up menus? These are normally called context menus in .NET. This is another great stride forward from VB 6.0.

VB 6.0 had no menu negotiation, only toolbar negotiation. This means that if you had a child window with its own menu, it would completely replace the main form's menu. This was a pain, but you could get around it. The real pain was in using pop-up menus.

In VB 6.0 a pop-up menu is taken from the main menu. If you had even an invisible menu for pop-up purposes on an MDI child form, it would wipe out the main form's menu. It took me a long time to develop a trick to get around this problem in VB 6.0 by using a surrogate form whose only purpose in life was to provide me with a pop-up menu that did not interfere with the main form's menu.

.NET has no such restriction. As you have seen with the main menu, creating a pop-up menu is just as easy and is not dependent upon any existing control.

The Payroll form has a label with a big dollar sign ($) on it. Type in the following code for the constructor for this form.

C#

  public Payroll()
  {
    InitializeComponent();

    ContextMenu m = new ContextMenu();
    m.MenuItems.Add("Add money to employee");
    m.MenuItems.Add("Remove money from employee");
    m.MenuItems.Add("Give raises to everyone");
    MenuItem mnu = m.MenuItems[m.MenuItems.Count-1];
    mnu.Enabled = false;
    m.MenuItems.Add("Cut in pay");

    lblCash.ContextMenu = m;
  }

VB

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    Dim m As ContextMenu
    m = New ContextMenu()
    m.MenuItems.Add("Add money to employee")
    m.MenuItems.Add("Remove money from employee")
    m.MenuItems.Add("Give raises to everyone")
    Dim mnu As MenuItem = m.MenuItems(m.MenuItems.Count - 1)
    mnu.Enabled = False
    m.MenuItems.Add("Cut in pay")

    lblCash.ContextMenu = m

  End Sub

As you can see, it is very easy to create a context menu that pops up when you right-click the dollar sign. This code has nothing to do with the main form.

Note 

The online help for .NET contains many examples of how to create and use MDI applications. You will also find quite a bit of help on how menus are handled in .NET.

This all I cover in this chapter with regard to creating SDI and MDI applications. As far as which one you should use for your data entry program, this up to you. I prefer an SDI application if the application will have only a few screens that can be filled in one at time. I do not like lots of screens littering my desktop. I use an MDI application if the application will have quite a few screens that can be open at the same time. I like to be able to minimize the whole application if I want.


Team LiB
Previous Section Next Section