
Add Main Menu
Hey, you know what else would be a neat thing for this
application to have? How about a menu? Drag one out of the tool box and drop it
on the application.

As always, the default name for the new menu will need to
be changed to something a bit more meaningful. Right click on the menu item and
select properties.

Give the menu the astoundingly clever and descriptive name
of mainMenu.

The main menu will need to give a user the means to specify
a database and close the application. Add File to the empty menu.
The default name for the menu item is menuItem1. As descriptive and
meaningful as that might be, let’s give it the new name of menuItemFile
at the same time that we give it a text string of File.
We also need to specify a MerdgeOrder value. This
value is used to determine the order in which various menus are merged together.
The C# environment merges MDI parent and MDI child menus together when dealing
with displaying menus that are appropriate to the currently active window. I’ll
explain more on this later when we are defining the MDI child menu.

Add the commands that will handle a user request to open a
database or close the application to the File popup menu item. The
interface gets a little slick here. Back in the VC++ world, a programmer would
have to manually specify if an item was a popup or not. Here, the interface
figures that out for you. If it doesn’t have a sub-item then it must be a menu
item. Otherwise, it must be a popup.
One little thing that we’re going to skip over right this
second is the MergeType and MergeOrder properties. We’ll get to
that later once we’ve started making a menu for the MDI child forms.

Add the new items and assert the following properties:
|
Name |
Shortcut |
Text |
|
menuDbOpen |
CtrlO |
&Open DB |
|
menuAppExit |
Alt4 |
E&xit |
Between the Open DB and Exit
menu items is a separator. Those of us coming from a Visual C++ background will
be stymied for a while trying to figure this one out. To insert a separator in a
menu, right click on the location where the separator is to go and select
‘Separator’ from the context menu.

You can build this application now but it won’t do anything
and, in fact, clicking on the Exit command does nothing at all.
Unlike VC++, there’s no default handler for pre-defined message IDs. Just in
case you haven’t been paying attention, we haven’t had any place to specify a
message ID. The menu commands get handled by explicitly attaching a command
handler directly to the menu object itself.
Let’s define a menu handler for the Exit
command. Double click on it. The IDE will generate an empty handler and move you
there. Add this.Close(); to the handler.
private void menuAppExit_Click(object
sender, System.EventArgs e)
{
this.Close();
}


|