Home > Articles

0672322714

Adding Menus

Now that you've created a toolbar for your application, you might need to add a menu to enable users to easily select many of the common functions. In most applications, you usually have file functions that allow users to create, edit, and save files. You also have edit functions that allow users to move data around and those for specific tasks in your application. For example, an application I've written has functions for handling pilot information, aircraft data, and log entry functions—in other words, these functions provide lots of things that the users can do.

One of the most important things in any application is allowing users to easily access all its functions. Users are accustomed to accessing most functions with a single mouse click. Also, most users want all the functions located conveniently in one place; to handle this in your application, use toolbars and menus. Visual Basic allows you to quickly and easily create menus with the new MainMenu control, which you create using the actual menu bars located at the top of a form. Pop-up menus that users typically access by right-clicking are created by using the new ContextMenu Control.

NOTE

If you've been using Visual Basic for any length of time, you might be asking, "Where is the Menu Editor?" Well, it's gone! The two new controls, MainMenu and ContextMenu replace the editor, making it easier to work with the menus directly on a form.

Creating an Application Menu

When creating any type of menu system for an application, you must determine what functions you want or need to put on the menu and how you want to organize these functions. By looking at Visual Basic's main menu, you can see that certain functions are organized into groups of similar items (see Figure 3.9).

Figure 3.9 Organizing menu items into functional groups.

It is important to group similar menu items according to the application you're creating. In fact, to be consistent with other Windows applications, you should use groups that your users are already familiar with. This way, they have an idea of where to find a particular menu item, even if they've never used this application before. You might find the following standard menu groups in a Windows application:

  • File—This menu contains any functions related to the opening and closing of files used by your application. Some standard items included in this menu are New, Open, Close, Save, Save As, Print, and Page Setup. The File menu also is the location of the most recently used file list that many applications have. Finally, a File menu generally appears where the Exit command is located.

  • Edit—The functions on this menu pertain to editing text and documents. Some typical Edit items are Undo, Cut, Copy, Paste, and Clear.

  • View—This menu can be included if your program supports different views for the same document. For example, a word processor might include a normal view for editing text and a page layout view for positioning document elements.

  • Tools—This menu is a catchall for any optional programs that might be available from within the application. For example, a spelling checker might be included for a word processor.

  • Window—If your application supports working with multiple documents at the same time, you should have this menu included in your application. The Window menu is set up to let users arrange the multiple documents or switch rapidly between them.

  • Help—The Help menu allows users to access your application's help system. It usually includes menu items for a table of contents, an index, a search feature, and an About box.

Use these six menu types as a starting point when creating the menu system for your application. You can include any of them as you need them, but don't think you need to add all six. Also, if you need other menu groups for your application, you can add whatever groups you might need.

NOTE

When adding other menu groups, be careful not to confuse your users. Whenever possible, place as many of your menu functions in one of the standard menu groups.

Building the Menu Groups

After you decide what functions you want to include in the menu and how to group them, you can start building the menu. When creating a menu for your application, remember that every form in the application can have its own menu defined. To see how to create a menu, start with the project you created previously for the toolbar example. (If you didn't save it, don't panic—just start a new project.)

When creating a menu, you must display the form that you want to add the menu to. Double-click the MainMenu control in the Toolbox to add it to the form. You will see the first menu item on the form ready for you to enter the text (see Figure 3.10).

Figure 3.10 Adding menu items to the Main Menu has never been easier. Simply type where it says, "Type Here."

When the MainMenu control is on the form, you can start adding the items that you want in the menu. For each item you want on a menu, you will enter the Text property directly on the menu control at the top of the form. However, the Name property must be changed separately. The Text property is what users see on the menu when using your application; the Name property is what you use in your application code to access the menu item. After you enter a menu item, use the arrow keys to move the cursor and accept the value for the item, moving you to the next menu item (see Figure 3.11).

Figure 3.11 The entered menu items on the form.

NOTE

Remember to change the Name property for each menu item. Otherwise, all menu items will look almost the same in the code, except the number added to the control name.

After you finish entering the menu items for the application, click away from the menu. Your menu will appear on the form exactly as you've entered it.

Adding Menu Levels

If you created a menu as shown in Figure 3.12, it probably will be very clumsy to work with. Every item that you've entered appears on the main menu bar.

Figure 3.12 A menu with all the items displayed on the main menu bar.

If your application has only one or two menu options, this might be acceptable; however, if you have many different menu items, the menu bar will run out of space. What you really need to do is set up a menu that uses multiple levels to display multiple menu items. When you click the menu item on the menu bar, the first level of the menu drops down (see Figure 3.13). This level is called a submenu.

Figure 3.13 Multiple levels of a menu item allow for many options in a small amount of menu space.

Adding subitems to the menu is very easy to do: First select the menu item you want to add the subitem to. This will display a new item entry to the right of the select item (see Figure 3.13). Now click the new subitem displayed and enter the required text. If you want to insert a new item in the menu list, right-click the item below the point where you want to insert the new item and select Insert Item from the pop-up menu.

One other option in a menu is a separator bar, which is a line that allows you to group different menu items together with a single menu item's sublevel list (see Figure 3.14). These bars break up a long list of menu items without creating submenus.

Adding a separator bar used to be a bit tricky. However, in Visual Basic .NET, the process has become extremely simple. To add a separator, select the item below the position where you want to add the separator and right-click; then select Insert Separator from the pop-up menu (as in Figure 3.14).

Figure 3.14 With separator bars, you can group and organize menu items at the same level.

NOTE

Although separator bars are great to use, don't try to add one to the top level of the menu. You can use them only in submenus.

Enhancing the Menu

If you've been working with Windows applications for any length of time, you've probably noticed that you can access a menu item in several different ways. Usually you can do this using a combination of keystrokes. You can include two different types of access in your menu: hotkeys and shortcuts.

Adding Hotkeys

Hotkeys are something you already know about and use, probably without thinking about it. A hotkey is identified by an underscore beneath the letter in the item's text (for example, the E in Edit). To create a hotkey, place an ampersand (&) in the Text property immediately before the letter you want as the hotkey. For the File menu item, the value of the Text property would be &File. Hotkeys can be used for any item in your menu, including the top-level menu items.

NOTE

At any given level of a menu, only one unique value can be used as a hotkey. For example, the Visual Basic menu has File and Format at the same top level. If you look closely at them, you'll see that the File menu item has the F as the hotkey, but the Format menu has the o as the hotkey. If you used the F for both menu items, Windows wouldn't know which one you really wanted. However, the same letter can be used in items that appear in different groups, such as the File menu's Print option and the View menu's Page Layout option. For each group or level, you can have at most 36 hotkeys—one for each letter and one for each number.

After you include hotkeys in your menu, you can open a top-level menu simply by holding down the Alt key and pressing the hotkey of choice. When the menu appears, you can press the hotkey for the menu item you want to execute. For example, if you want to start a new project in Visual Basic, you could press Alt+F and then N for the File menu's New Project option.

TIP

If there's no conflict with letter selection, you should use the first letter of a menu item as the hotkey. This is what users expect; it also makes it easy to guess what the hotkey is for a particular function.

Adding Shortcuts

The other way to provide menu access is with shortcuts. Shortcut keys provide direct access to any function in a menu, no matter what level it's actually on. You can perform shortcuts with a key combination (such as Ctrl+C for a copy function) or with a single key (such as Delete for the delete function). If you use the default shortcut keys (listed in Table 3.3), users will already know how to perform certain common tasks.

Table 3.3  Standard Shortcut Keys Used by Windows Applications

Menu Item

Shortcut Key

Description

Edit Menu

Cut

Ctrl+X

Removes text or a control and copies it to the Clipboard

Copy

Ctrl+C

Copies the text or control to the Clipboard

Paste

Ctrl+V

Pastes the contents of the Clipboard to the selected area

Undo

Ctrl+Z

Undoes the last change

Find

Ctrl+F

Finds a string

File Menu

Open

Ctrl+O

Opens a file

Save

Ctrl+S

Saves the current file

Print

Ctrl+P

Prints the current data

Assigning shortcut keys is simply a process of selecting the key or keys that you want to use from the Shortcut drop-down list. Shortcut keys are displayed to the right of the menu item both in the selection list (see Figure 3.15) in the Menu Editor and in the actual menu in your application.

Figure 3.15 Shortcuts are displayed in the menu item selection list.

Because of the way shortcut keys work, only one shortcut key can use a given key combination. There also is a new property for each menu item that allows you to choose whether a shortcut key is displayed to the user at any point in the application.

TIP

Just like with hotkeys, the shortcut key should correspond with the first letter of the menu item whenever possible.

Adding the Menu Code

Adding code for the menu is the same as adding code for any other control in your application. There is a Click event for each menu item added to the form. For this reason, giving each menu item a unique name is very important. The Click event is triggered whenever you select the menu item by clicking it, by using the hotkey or the shortcut key. To display the Click event routine for a menu item, simply double-click the menu item you want to work with; its related Click event will appear in the Code Editor window.

NOTE

If you look at the code that you added to the dlgFileOpenCopy project on Day 2, notice that you had code in the mnuFileOpen_Click routine.

Optional Menu Settings

In addition to the two required properties, each menu item has several other optional properties that you can use to control the menu item:

  • Checked determines whether a check mark is displayed next to a menu item when it's selected. This is used to indicate that a particular option has been selected.

  • RadioCheck determines whether a dot is displayed instead of a check mark.

  • If Enabled is false (not selected), users can see the menu item but can't access it.

  • Visible allows you to hide menu items that aren't needed for a particular function or form.

  • MergeType determines whether the top-level menu items are displayed while another form is active and contains its own menu.

  • MergeOrder determines the actual position of the menu item when the MergeType is MergeItems.

  • MDIList is available only when with MDI applications. It displays a list of the current child windows displayed.

When you want to indicate the status of a particular function, use the Checked property to show a check mark in the menu drop-down list for the item selected. Visual Basic uses this property to show which toolboxes are displayed (see Figure 3.16). This property actually toggles back and forth between True and False within the application code itself.

Figure 3.16 Using the Checked property to display a function's status.

To use the Checked property, add the following code to the Click event of any menu item that with which you want to use it:

If <menu item>.Checked then
    <menu item>.Checked = True
Else
    <menu item>.Checked = False
End If

NOTE

If you try to use the Checked property for a top-level menu item, you'll get an error message. You can't toggle top-level menu items on and off.

The MDIList property specifies that a list of open MDI child forms will be displayed. When this property is set to True, the menu automatically adds items as child forms are opened and removes any closed child forms.

Creating and Using Context Menus

Menus generally are thought of as being visible and occupying the bar at the top of an application form. Visual Basic allows you to create another type of menu, which appears only when needed and directly next to the area you're working with. These context menus often are used to handle functions related to a specific area of the form. For an example of this type of menu, right-click anywhere in Visual Basic's Code Editor window for a code-related menu (see Figure 3.17). Context menus usually appear at the mouse pointer's current location. After you select an option from the menu, it disappears from the screen.

Figure 3.17 Context menus offer users many options directly related to the work they're performing.

Setting Up a Context Menu

To create a context menu, add the ContextMenu control to the form; then add all the required menu items using the same techniques as the MainMenu control.

NOTE

The context menu is new to Visual Basic .NET. It has been separated from the Main menu so it's handled independently in the code.

CAUTION

If you want a menu item to appear on the main menu and on the context menu, you must add the same item twice: once on the main menu and once on the context menu.

Displaying a Context Menu

To see how the context menu works, add one to your form and insert at least one menu item. The final step is changing the ContextMenu property for the control with which you want to associate the context menu. Once this is done, you can execute the application and right-click the control to display the context menu, as shown in Figure 3.18.

Figure 3.18 Using context menus in your application.

Merging Menus

What does merging menus really do for you? When you're creating an MDI application, you must take care to create your menus properly. If any of the child forms have menus of their own, you must decide which menu bar will be displayed by your application. Whenever a child form has a menu associated with it, its menu will replace the parent form menu when the child form is displayed.

You can deal with menus in an MDI application in one of the following MergeTypes:

  • Add  The menu item will be added to the existing menu displayed.

  • Replace  The menu item replaces an existing menu item at the same position in a merged menu.

  • MergeItems  All submenu items are merged with the existing menu items at the same position in a merged menu.

  • Remove  The menu item isn't included in the merged menu.

The MergeOrder property specifies the position a menu item would be placed in when merged with an existing menu on a parent form.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020