Поделиться через


Walkthrough: Providing Standard Menu Items to a Form

You can provide a standard menu for your forms with the MenuStrip control.

This walkthrough demonstrates how to use a MenuStrip control to create a standard menu. The form also responds when a user selects a menu item. The following tasks are illustrated in this walkthrough:

  • Creating a Windows Forms project.

  • Creating a standard menu.

  • Creating a StatusStrip control.

  • Handling menu item selection.

When you are finished, you will have a form with a standard menu that displays menu item selections in a StatusStrip control.

To copy the code in this topic as a single listing, see How to: Provide Standard Menu Items to a Form.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

Prerequisites

In order to complete this walkthrough, you will need:

  • Sufficient permissions to be able to create and run Windows Forms application projects on the computer where Visual Studio is installed.

Creating the Project

The first step is to create the project and set up the form.

To create the project

  1. Create a Windows application project called StandardMenuForm.

    For more information, see How to: Create a New Windows Forms Application Project.

  2. In the Windows Forms Designer, select the form.

Creating a Standard Menu

The Windows Forms Designer can automatically populate a MenuStrip control with standard menu items.

To create a standard menu

  1. From the Toolbox, drag a MenuStrip control onto your form.

  2. Click the MenuStrip control's smart tag glyph (Smart Tag Glyph) and select Insert Standard Items.

    The MenuStrip control is populated with the standard menu items.

  3. Click the File menu item to see its default menu items and corresponding icons.

Creating a StatusStrip Control

Use the StatusStrip control to display status for your Windows Forms applications. In the current example, menu items selected by the user are displayed in a StatusStrip control.

To create a StatusStrip control

  1. From the Toolbox, drag a StatusStrip control onto your form.

    The StatusStrip control automatically docks to the bottom of the form.

  2. Click the StatusStrip control's drop-down button and select StatusLabel to add a ToolStripStatusLabel control to the StatusStrip control.

Handling Item Selection

Handle the DropDownItemClicked event to respond when the user selects a menu item.

To handle item selection

  1. Click the File menu item that you created in the Creating a Standard Menu section.

  2. In the Properties window, click Events.

  3. Double-click the DropDownItemClicked event.

    The Windows Forms Designer generates an event handler for the DropDownItemClicked event.

  4. Insert the following code into the event handler.

    ' This method is the DropDownItemClicked event handler.
    ' It passes the ClickedItem object to a utility method
    ' called UpdateStatus, which updates the text displayed 
    ' in the StatusStrip control.
    Private Sub FileToolStripMenuItem_DropDownItemClicked( _
    ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) _
    Handles FileToolStripMenuItem.DropDownItemClicked
    
        Me.UpdateStatus(e.ClickedItem)
    
    End Sub
    
    // This method is the DropDownItemClicked event handler.
    // It passes the ClickedItem object to a utility method
    // called UpdateStatus, which updates the text displayed 
    // in the StatusStrip control.
    private void fileToolStripMenuItem_DropDownItemClicked(
        object sender, ToolStripItemClickedEventArgs e)
    {
        this.UpdateStatus(e.ClickedItem);
    }
    
  5. Insert the UpdateStatus utility method definition into the form.

    ' This utility method assigns the value of a ToolStripItem
    ' control's Text property to the Text property of the 
    ' ToolStripStatusLabel.
    Private Sub UpdateStatus(ByVal item As ToolStripItem)
    
        If item IsNot Nothing Then
    
            Dim msg As String = String.Format("{0} selected", item.Text)
            Me.StatusStrip1.Items(0).Text = msg
    
        End If
    
    End Sub
    
    // This utility method assigns the value of a ToolStripItem
    // control's Text property to the Text property of the 
    // ToolStripStatusLabel.
    private void UpdateStatus(ToolStripItem item)
    {
        if (item != null)
        {
            string msg = String.Format("{0} selected", item.Text);
            this.statusStrip1.Items[0].Text = msg;
        }
    }
    

Checkpoint

To test your form

  1. Press F5 to compile and run your form.

  2. Click the File menu item to open the menu.

  3. On the File menu, click one of the items to select it.

    The StatusStrip control displays the selected item.

Next Steps

In this walkthrough, you have created a form with a standard menu. You can use the ToolStrip family of controls for many other purposes:

See Also

Reference

MenuStrip

ToolStrip

StatusStrip

Other Resources

MenuStrip Control (Windows Forms)