Export (0) Print
Expand All

Setting the Text Displayed by a Windows Forms Control

Visual Studio .NET 2003

Windows Forms controls normally 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, the text can be set or returned using the Text property. The font can be changed using the Font property.

To set the text displayed by a control in the designer

  • Using the Properties window, set the Text property of the control to an appropriate string. To create an underlined access key, includes an ampersand (&) before the letter that will be the access key.
  • Using the Properties window, click the ellipsis button (Aa983721.vbellipsesbutton(en-us,VS.71).gif) next to the Font property. A standard font dialog box appears, allowing you to select the font, font style, size, effects (such as strikeout or underline), and script.

To set the text displayed by a control programmatically

  • 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.
  • Set the Font property to an object of type System.Drawing.Font.
    ' Visual Basic
    Button1.Text = "Click here to save changes"
    Button1.Font = New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    
    // C#
    button1.Text = "Click here to save changes";
    button1.Font = new Font("Arial", 10, FontStyle.Bold,
       GraphicsUnit.Point);
    
    // C++
    button1->Text = S"Click here to save changes";
    button1->Font = new System::Drawing::Font("Arial",
       10, FontStyle::Bold, GraphicsUnit::Point);
    

See Also

Creating Access Keys for Windows Forms Controls | Responding to Windows Forms Button Clicks | Control.Text Property

Show:
© 2014 Microsoft