共用方式為


如何:設定控制項所顯示的文字 (Windows Forms .NET)

Windows Forms 控制項通常會顯示一些與控件主要功能相關的文字。 例如,Button控件通常會顯示 標題,指出按下按鈕時將執行哪些動作。 針對所有控制項,您都可以使用 Text 屬性來設定或傳回該文字。 您可以使用 Font 屬性來變更字型。

您也可以使用 設計工具來設定文字。

重要

.NET 7 和 .NET 6 的桌面指南檔正在建置中。

設計工具

  1. 在 Visual Studio 的 [ 屬性 ] 視窗中,將 控件的 Text 屬性設定為適當的字串。

    若要建立加底線的快速鍵,請在將做為快捷鍵的字母前面加上連字元 (&) 。

    Visual Studio Properties pane for .NET Windows Forms with Text property shown.

  2. 在 [屬性] 視窗中,選取 Font 屬性旁邊的省略號按鈕 ()。

    Visual Studio Properties pane for .NET Windows Forms with Font property shown.

    在標準字型對話框中,使用類型、大小和樣式等設定來調整字型。

    Visual Studio Properties pane for .NET Windows Forms with Font settings window.

程式設計

  1. Text 屬性設為字串。

    若要建立加底線的存取金鑰,請在將成為存取金鑰的字母前面加上連字元 (&) 。

  2. Font 屬性設為 Font 類型的物件。

    Button1.Text = "Click here to save changes"
    Button1.Font = New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    
    button1.Text = "Click here to save changes";
    button1.Font = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point);
    

    注意

    您可以在使用者介面項目中使用逸出字元來顯示特殊字元,這些使用者介面項目 (例如功能表項目) 通常會以不同方式來解譯該字元。 例如,下列程式代碼行會將功能表項的文字設定為「現在為完全不同的專案」:

    MPMenuItem.Text = "&& Now For Something Completely Different"
    
    mpMenuItem.Text = "&& Now For Something Completely Different";
    

另請參閱