The MainMenu Object

Your control, mainMenu1, is of type System.Windows.Forms.MainMenu. This class represents the container for the entire menu structure within a form. While the MainMenu class has a number of properties, the one you will use most often is the MenuItems property, which returns a collection of MenuItem objects. A menu consists of these MenuItem objects, each of which represents one choice on the menu.

You will find that when you click on the form, the menu disappears! When you click on mainMenu1, the menu reappears, in place, ready for you to edit. The menu is actually independent of the form; it is shown in its place as a convenience for you. However, if you run the application now, the menu will not appear—it is not yet attached to the form.

You can attach the menu to the form in several ways. The easiest is to click on the form to bring up its Properties window, and then scroll down to the Menu property. There you can choose mainMenu1, associating the menu with the form itself, as shown in Figure 18-2.

Associating the menu with the form

Figure 18-3. Associating the menu with the form

When you click on the form, the menu will no longer disappear; it is now a property of the form itself. This is reflected in the initialization code of the form:

image with no caption

this.Menu = this.mainMenu1;

me.Menu = me.mainMenu1

Adding ...

Get Programming .NET Windows Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.