Menus

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 8.  Using Controls


Menus

Menus are one of the primary ways that users interact with many applications. Each window can display its own menu, although it is more common to find a menu only on the main window. Each window can also display a context menu, if the programmer so chooses. A context menu is one that is displayed when the user right-clicks with the mouse to display a popup menu.

MainMenu Control

The MainMenu control can be used to design, display, and manage a menu that is attached to the top of the client area of the window. We demonstrate the use of menus by building a new version of the HotelAdmin program we saw in the previous chapter. In this example, named HotelAdmin2 , we have the following main menu structure:

 graphics/codeexample.gif  F  ile        Ho  t  els  H  elp    E  x  it  A  dd  A  bout...  E  dit  D  elete 

The underscore underneath a letter of the menu caption identifies the letter as a menu shortcut. You can press the ALT key and the letter to select the menu containing that shortcut. If a menu is already displayed, an item can be selected using only its shortcut key. For example, the key sequence ALT+T+E generates the same event as if you select the Hotels Edit menu using the mouse. To define a menu shortcut key, you must place an ampersand (&) in the caption before the letter that is the shortcut key. For example, "E&xit" would underline the letter x on the menu and associate it as the shortcut key.

Menu items may also have an accelerator key attached to them. For example, F4 might be the equivalent of Add, or CTRL+D might be the equivalent of Exit. Whereas a shortcut must use a letter from the caption as the shortcut key, an accelerator is free to use a combination of CTRL, ALT, SHIFT, and keys from the keyboard. The accelerator key is assigned by setting the Shortcut property of a menu item.

Step 1: Using the MainMenu Control

The MainMenu control is used to design menus. As you can see in Figure 8-1, we have modified the startup form for the application by removing the command buttons and placing a MainMenu control on the form. It has been named it mnuHotelBroker . When it is drawn on the form, an icon appears in a separate window where other special controls such as the ErrorProvider and ToolTip controls are placed. It has also attached a menu to the form that we can now modify.

Figure 8-1. Using the MainMenu control.

graphics/08fig01.jpg

Each item on the menu has a set of properties associated with it that can be manipulated using the Properties window. The properties unique to the MainMenu control include:

  • Checked indicates whether a check mark is next to menu caption.

  • RadioChecked indicates whether a round check mark is next to the menu caption.

  • Shortcut indicates the accelerator key (Ctrl-X, etc.) that should be assigned to the menu item.

  • ShortcutVisible indicates whether the accelerator identity is displayed next to the menu caption.

Table 8-1 shows the property values used to define the menu shown at the beginning of this section. To set these properties, click on the menu that is attached to the form. Enter the Text property value in the area displayed. You can then use the Properties window to set the remaining properties.

Table 8-1. Property Values for the MainMenu Control in the HotelBroker2 Startup Form
Text Name Shortcut
&File mnuFile  
E&xit mnuExit CtrlD
Ho&tel mnuHotels  
&Add mnuAdd  
&Edit mnuEdit  
&Delete mnuDelete  
&Help mnuHelp  
&About... mnuAbout  

Step 2: Responding to Menu Events

Each menu item has a Click event associated with it. It must be handled in order to provide a response to the user's selection of that menu item. For example, the Click event handlers for the HotelAdmin2's Exit and About menus are shown below. The handler for Exit closes MainForm . The handler for About displays a message box.

 Private Sub mnuExit_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles mnuExit.Click  Me.Close()  End Sub Private Sub mnuAbout_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles mnuAbout.Click  MessageBox.Show("Hotel Broker Administration v1.1", _   "About HotelAdmin", MessageBoxButtons.OK, _   MessageBoxIcon.Information)  End Sub 

In the event handlers for each of the three items on the Hotels menu, we have added code to display the appropriate dialog. (We simply moved the code in the Click event of the buttons from last chapter's HotelAdmin example into the Click event of the respective menu.) Figure 8-2 shows the new GUI for our HotelAdmin2 application.

Figure 8-2. HotelAdmin2's startup form.

graphics/08fig02.jpg

Step 3: Controlling Menu Appearance

Menu items have properties such as Checked , Enabled and Visible that can be used to control their appearance on the menu. In our HotelAdmin2 application, we do not want the users to be able to select Edit or Delete if they do not have any hotel in the listbox selected, so we must enable or disable those items programmatically.

We will handle the Popup event of mnuHotels . This event is generated when the user selects Hotels but before the associated dropdown menu appears. In it, we will enable or disable the appropriate items under mnuHotels before they are displayed.

 Private Sub mnuHotels_Popup(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles mnuHotels.Popup  If lstHotels.SelectedIndex <>   -1 Then   mnuEdit.Enabled = True   mnuDelete.Enabled = True   Else   mnuEdit.Enabled = False   mnuDelete.Enabled = False   End If  End Sub 

Figure 8-3 shows the Hotels dropdown when there is no selected hotel. As you can see, the Edit and Delete menu items are disabled.

Figure 8-3. Controlling menu appearance.

graphics/08fig03.jpg

ContextMenu Control

The ContextMenu control can be used to display a floating popup menu that is displayed when the user right-clicks on a control or other area of the form. Typically, context menus provide shortcuts to commonly used items on the main menu. In the HotelAdmin2 program, we will provide a context menu with the three Hotel menu options (Add, Edit and Delete). To add a context menu for a form, we must perform the following actions:

  • Add the ContextMenu control to the form and set its Name property to mnuContext .

  • Set the ContextMenu property of the form to mnuContext . (See Figure 8-4.)

    Figure 8-4. Using the ContextMenu control.

    graphics/08fig04.jpg

  • Add menu items to the context menu and associate them with an event handler. We do this programmatically. If the menu item's Click event cannot be associated with an existing handler, we must also write the handler function(s).

We will initialize our context menu when the form loads because the items on the menu are always the same, regardless of where the user right-clicks or what data is on the form.

All menu controls have a MenuItems property that represents the collection of items on the menu. Using this property, you can access methods such as Add and Remove to manipulate the items on the menu. The code for our form's Load event handler is shown below. It adds three items to the context menu and associates a Click event handler function with each.

 Private Sub MainForm_Load(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles MyBase.Load  mnuContext.MenuItems.Add("Add Hotel", _   New EventHandler(AddressOf mnuAdd_Click))   mnuContext.MenuItems.Add("Edit Selected Hotel", _   New EventHandler(AddressOf mnuEdit_Click))   mnuContext.MenuItems.Add("Delete Selected Gotel", _   New EventHandler(AddressOf mnuDelete_Click))  End Sub 

Because the main menu already had event handlers that responded to Add, Edit and Delete selections from the menu, we used those handlers for the corresponding context menu item handlers. If we did not have existing handler functions that could have been associated with the context menu's Click event, we would have built a standard Click event handler and associated it as the handler procedure. In this version of the form's Load event handler, we associated the ClickedOnContextMenu procedure as the event handler for all menu items on the context menu.

 Private Sub MainForm_Load(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles MyBase.Load    mnuContext.MenuItems.Add("Add Hotel", _       New EventHandler(AddressOf ClickedOnContextMenu))    mnuContext.MenuItems.Add("Edit Selected Hotel", _       New EventHandler(AddressOf ClickedOnContextMenu))    mnuContext.MenuItems.Add("Delete Selected Hotel", _       New EventHandler(AddressOf ClickedOnContextMenu)) End Sub 

The selection of any item on the context menu will send us to the ClickedOnContextMenu functions shown below. To determine which menu selection caused the Click handler to be invoked we could have referenced the handler's System.Object parameter. All Click event handlers are passed a System.Object parameter and a System.EventArgs parameter. The System.Object parameter, named sender by the wizard that writes handler functions, identifies the control that caused the event. In the ClickedOnContectMenu handler we would write because of the last version of our Load event handler, we would convert the sender parameter to a MenuItem parameter using CType . We would then extract the Text property of the menu item to determine which item from the context menu was selected.

 Private Sub ClickedOnContextMenu(ByVal sender As _  System.Object, ByVal e As System.EventArgs)    Dim mnuSelected As MenuItem = CType(sender, MenuItem)  If mnuSelected.Text = "Add Hotel" Then   ' Code to handle Add goes here   ElseIf mnuSelected.Text = "Edit Selected Hotel" Then   ' Code to handle Edit goes here   ElseIf mnuSelected.Text = "Delete Selected Hotel" Then   ' Code to handle Delete goes here   End If  End Sub 

If we want to know when the context menu is being displayed so that we may set check marks or disable menu items, we must handle the content menu's PopUp event. Inside this handler, we can determine the state of the application and modify the menu's appearance as needed. Items on the context menu can be accessed via the subscriptable MenuItems property.

To complete our HotelAdmin2 program, we will disable the Edit and Delete menus when they are displayed on the context menu. See Figure 8-5.

Figure 8-5. Controlling ContextMenu appearance.

graphics/08fig05.jpg

 Private Sub mnuContext_Popup(ByVal sender As _  System.Object, ByVal e As System.EventArgs) _  Handles mnuContext.Popup  If lstHotels.SelectedIndex <>   -1 Then   ' Enable Edit and Delete   mnuContext.MenuItems(1).Enabled = True   mnuContext.MenuItems(2).Enabled = True   Else   ' Disable Edit and Delete   mnuContext.MenuItems(1).Enabled = False   mnuContext.MenuItems(2).Enabled = False   End If  End Sub 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net