Export (0) Print
Expand All

How to: Display Text on a Windows Form

Visual Studio 2008

You can display text on a variety of controls, but the primary control for displaying text in your program is the Label control. When you add a label to a form, the background appears in the same color as the form so that only the text is visible. You can also change the BackColor property of the label.

You can display text in the label by setting its Text property. The Font property determines the display font for the text in the Text property. The ForeColor property determines the color of the text itself.

To display text in a label

  1. On the File menu, click New Project.

  2. In the New Project dialog box, click Windows Forms Application, and then click OK.

    A new Windows Forms project opens.

  3. From the Toolbox, drag a Label onto the form.

  4. Add a Button control to the form, and change the following properties:

    Property

    Value

    Name

    changeText

    Text

    Change Text

    Size

    80, 23

  5. Double-click the button to create the changeText_Click event handler, and add the following code:

    this.label1.Text = "Time " + DateTime.Now.ToLongTimeString();
    
  6. Add another Button control to the form, and change the following properties

    Property

    Value

    Name

    changeColor

    Text

    Change Color

    Size

    80, 23

  7. Double-click the button to create the changeColor_Click event handler, and add the following code:

    Random randomColor = new Random();
    this.label1.ForeColor = Color.FromArgb(randomColor.Next(0, 256),
        randomColor.Next(0, 256), randomColor.Next(0, 256));
    
  8. Press F5 to run the program.

  9. Click Change Text and verify that the text in the label is updated.

  10. Click Change Color and verify that the font of the text is changed to a new color.

Community Additions

ADD
Show:
© 2014 Microsoft