Share via


How to: Set the Text Displayed by a Windows Forms Control

Windows Forms controls usually display some text that is related to the primary function of the control. For example, a Button control usually displays a caption indicating what action will be performed when the button is clicked. For all controls, you can set or return the text by using the Text property. You can change the font by using the Font property. You can also set the text using the designer. For more information, see How to: Create Access Keys for Windows Forms Controls Using the Designer and How to: Set the Text Displayed by a Windows Forms Control Using the Designer and How to: Set the Image Displayed by a Windows Forms Control Using the Designer and How to: Create Access Keys for Windows Forms Controls Using the Designer and How to: Set the Text Displayed by a Windows Forms Control Using the Designer and How to: Set the Image Displayed by a Windows Forms Control Using the Designer and How to: Create Access Keys for Windows Forms Controls Using the Designer and How to: Set the Text Displayed by a Windows Forms Control Using the Designer and How to: Set the Image Displayed by a Windows Forms Control Using the Designer and How to: Create Access Keys for Windows Forms Controls Using the Designer and How to: Set the Text Displayed by a Windows Forms Control Using the Designer and How to: Set the Image Displayed by a Windows Forms Control Using the Designer..

To set the text displayed by a control programmatically

  1. Set the Text property to a string.

    To create an underlined access key, includes an ampersand (&) before the letter that will be the access key.

  2. Set the Font property to an object of type 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);
    
    button1.set_Text("Click here to save changes");
    button1.set_Font(new Font("Arial", 10, FontStyle.Bold,
       GraphicsUnit.Point));
    
    button1->Text = "Click here to save changes";
    button1->Font = new System::Drawing::Font("Arial",
       10, FontStyle::Bold, GraphicsUnit::Point);
    

    Note

    You can use an escape character to display a special character in user-interface elements that would normally interpret them differently, such as menu items. For example, the following line of code sets the menu item's text to read "& Now For Something Completely Different":

    MPMenuItem.Text = "&& Now For Something Completely Different"
    
    mpMenuItem.Text = "&& Now For Something Completely Different";
    
    mpMenuItem.set_Text("&& Now For Something Completely Different");
    
    mpMenuItem->Text = "&& Now For Something Completely Different";
    

See Also

Tasks

How to: Create Access Keys for Windows Forms Controls

How to: Respond to Windows Forms Button Clicks

Reference

Control.Text