Closer Look: More About Menus

In this lesson, you will learn how to enable or disable menus at run time, and also how to create pop-up menus.

In the previous lesson, you learned how to use the MenuStrip control to create menus that enable users to make choices regarding your program. However, in some cases choices may only be available at certain times. For example, a Copy menu command would only be available when there is something that can be copied.

Most programs disable, rather than hide, menu commands when they aren't available. When a menu item is disabled, the color of the menu text is changed to gray, and clicking the menu item does nothing. When using a MenuStrip control, you can disable and enable menu items by using the Enabled property of the MenuItem.

To disable or enable menu items

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Forms Application.

  3. In the Name box, type Menus2, and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a MenuStrip control and a TextBox control onto the form.

  5. In the form, click the MenuStrip control and type Edit, and then press ENTER.

  6. In the box underneath the first box, type Copy, and then press ENTER.

  7. In the Properties window, set the Enabled property of the CopyToolStripMenuItem to False.

  8. Double-click the TextBox control to open the Code Editor.

  9. In the TextBox1_TextChanged event handler, type the following code.

    
    If Textbox1.Text <> "" Then
      CopyToolStripMenuItem.Enabled = True
    Else
      CopyToolStripMenuItem.Enabled = False
    End If
    
    
    
  10. Press F5 to run your program. Click the Edit menu—the Copy menu item is disabled. Type something into the TextBox control, and then click the Edit menu again—the Copy menu item is now enabled.

Many programs use pop-up menus, also known as context menus, to provide easy access to frequently used commands. You access a context menu by right-clicking a form or control at run time. You can create your own context menus in Visual Basic using a ContextMenuStrip control.

As with the MenuStrip control, when you drag a ContextMenuStrip control onto a form, the ContextMenuStrip control appears as a box in the upper part of the form with the words "Type Here" inside, and an icon is added to the component tray. Unlike MenuStrip, additional items can only be added under the first menu item, creating a vertical menu.

In addition, a ContextMenuStrip has to be associated with the form or control where you want it to appear. You do this by setting the ContextMenuStrip property of the form or control to the name of the ContextMenuStrip control. You can associate a single ContextMenuStrip with as many controls as you like.

To create a context menu

  1. On the File menu, point to New and then click Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Forms Application.

  3. In the Name box, type ContextMenus and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a ContextMenuStrip control onto the form.

  5. Click the form in Design view. In the Properties window, select the ContextMenuStrip property of the form and then click ContextMenuStrip1 in the drop-down list.

  6. In the component tray at the bottom of the integrated development environment (IDE), click the ContextMenuStrip control. In the ContextMenuStrip, type Option1, and then press ENTER.

  7. In the box underneath the first box, type Option2, and then press ENTER.

  8. Double-click the Option1 menu item to open the Code Editor.

  9. In the Option1ToolStripMenuItem_Click event handler, type the following code.

    
    MsgBox("You chose Option 1")
    
    
    
  10. In the Code Editor, select Option2ToolStripMenuItem in the left-hand drop down box, and then select Click from the right-hand drop down box.

    A new event handler called Option2ToolStripMenuItem _Click appears in the code editor.

  11. In the Option2ToolStripMenuItem _Click event handler, type the following code.

    
    MsgBox("You chose Option 2")
    
    
    
  12. Press F5 to run your program. Right-click the form and click one of the items on the context menu—a message box appears that states which option you chose.

In this lesson, you learned how to enable and disable menus, and also how to create a context menu. In the next topic, you will learn how to use a different type of control—a Timer—to perform actions.

Next Lesson: Using Timers to Perform Regular Actions

Show: