共用方式為


逐步解說:對表單提供標準功能表項目

您可以使用 MenuStrip 控制項在表單中提供標準的功能表。

此逐步解說將示範如何使用 MenuStrip 控制項來建立標準功能表。 當使用者選取一個功能表項目時,表單也會有回應。 下列工作會在逐步解說中說明:

  • 建立 Windows Form 專案

  • 建立標準功能表

  • 建立 StatusStrip 控制項

  • 處理功能表項目的選取動作

當您完成時,您的表單將具備一個標準功能表,並且以 StatusStrip 控制項來顯示選取的功能表項目。

若要將此主題中的程式碼複製為一份清單,請參閱 HOW TO:對表單提供標準功能表項目

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

必要條件

若要完成這個逐步解說,您必須要有:

  • 具有足夠的權限,以便能夠在安裝了 Visual Studio 的電腦上建立及執行 Windows Form 應用程式專案

建立專案

第一個步驟是建立專案並設定表單。

若要建立專案

  1. 建立稱為 StandardMenuForm 的 Windows 應用程式專案。

    如需詳細資訊,請參閱 HOW TO:建立新的 Windows Form 應用程式專案

  2. 在 [Windows Form 設計工具] 中選取表單。

建立標準功能表

Windows Form 設計工具可以自動在 MenuStrip 控制項中填入標準功能表項目。

若要建立標準功能表

  1. 從 [工具箱] 將 MenuStrip 控制項拖曳到表單上。

  2. 按一下 MenuStrip 控制項的智慧標籤圖像 (智慧標籤圖像) 並選取 [插入標準項目]。

    MenuStrip 控制項中會填入標準的功能表項目。

  3. 按一下 [檔案] 功能表項目,檢查其預設的功能表項目和對應的圖示。

建立 StatusStrip 控制項

您可以使用 StatusStrip 控制項來顯示 Windows Form 應用程式的狀態。 在這個範例中,使用者所選取的功能表項目會顯示在 StatusStrip 控制項中。

若要建立 StatusStrip 控制項

  1. 從 [工具箱] 將 StatusStrip 控制項拖曳到表單上。

    StatusStrip 控制項會自動停駐在表單下方。

  2. 按一下 StatusStrip 控制項的下拉式按鈕,選取 [StatusLabel] 以將一個 ToolStripStatusLabel 控制項加入至 StatusStrip 控制項。

處理項目的選取動作

處理 DropDownItemClicked 事件以回應使用者對功能表項目的選取動作。

若要處理項目選取動作

  1. 按一下您在 [建立標準功能表] 區段中建立的 [檔案] 功能表項目。

  2. 在 [屬性] 視窗中按一下 [事件]。

  3. 按兩下 DropDownItemClicked 事件。

    Windows Form 設計工具會為 DropDownItemClicked 事件產生一個事件處理常式。

  4. 將下列程式碼插入至事件處理常式。

    ' 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. 將 UpdateStatus 公用程式方法定義插入至表單中。

    ' 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;
        }
    }
    

檢查點

若要測試您的表單

  1. 按 F5 編譯及執行您的表單。

  2. 按一下 [檔案] 功能表項目以開啟功能表。

  3. 在 [檔案] 功能表上,按一下其中一個項目將其選取。

    StatusStrip 控制項會顯示選取的項目。

後續步驟

在這個逐步解說中,您建立了一個使用標準功能表的表單。 您可以將 ToolStrip 系列的控制項使用在許多其他用途上:

請參閱

參考

MenuStrip

ToolStrip

StatusStrip

其他資源

MenuStrip 控制項 (Windows Form)