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

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

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

  • 建立 Windows Forms 專案。

  • 建立標準功能表。

  • 建立 StatusStrip 控制項。

  • 處理功能表項目選取專案。

當您完成時,會有一個具有標準功能表的表單,可顯示控制項中的 StatusStrip 功能表項目選取專案。

若要將本主題中的程式碼複製為單一清單,請參閱 如何:將標準功能表項目提供給表單

必要條件

您將需要 Visual Studio 才能完成本逐步解說。

建立專案

  1. 在 Visual Studio 中,建立名為 StandardMenuForm 的 Windows 應用程式專案( 檔案 > 新 > 專案 > Visual C# Visual Basic > 傳統型 > Windows Forms 應用程式)。

  2. 在 Windows Forms 設計工具中,選取表單。

建立標準功能表

Windows Forms 設計工具可以使用標準功能表項目自動填入 MenuStrip 控制項。

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

  2. MenuStrip按一下控制項的設計工具動作圖像 ( Small black arrow ),然後選取 [ 插入標準專案 ]。

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

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

建立 StatusStrip 控制項

StatusStrip使用 控制項來顯示 Windows Forms 應用程式的狀態。 在目前的範例中,使用者選取的功能表項目會顯示在 控制項中 StatusStrip

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

    控制項 StatusStrip 會自動停駐在表單底部。

  2. StatusStrip按一下控制項的下拉式按鈕,然後選取 [ 狀態][標籤 ],將控制項新增 ToolStripStatusLabelStatusStrip 控制項。

處理專案選取

DropDownItemClicked處理當使用者選取功能表項目時要回應的事件。

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

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

  3. 按兩下 DropDownItemClicked 事件。

    Windows Forms 設計工具會產生 事件的事件處理常式 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 void fileToolStripMenuItem_DropDownItemClicked(
        object sender, ToolStripItemClickedEventArgs e)
    {
        this.UpdateStatus(e.ClickedItem);
    }
    
    ' 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
    
  5. UpdateStatus 公用程式方法定義插入表單中。

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

檢查點 -測試表單

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

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

  3. 在 [ 檔案] 功能表上,按一下其中一個專案加以選取。

    控制項 StatusStrip 會顯示選取的專案。

下一步

在本逐步解說中,您已建立具有標準功能表的表單。 您可以針對許多其他用途使用 ToolStrip 控制項系列:

另請參閱