How to: Use TextBox Controls to Get User Input

You can both display text and retrieve text from users by using a TextBox control. After a user types data in a TextBox, you can retrieve this data by using the Text property. By default, the Multiline property of the TextBox is set to false. This means that users cannot press the ENTER key to create multiple lines of text in the TextBox. You can set the Multiline property to true to enable this.

To retrieve input typed in a text box

  1. On the File menu, click NewProject.

  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 TextBox control onto the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    inputText

    Multiline

    True

    Size

    175, 90

  4. Add a Button control next to the text box, and change the following properties:

    Property

    Value

    Name

    retrieveInput

    Text

    Retrieve

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

    MessageBox.Show(this.inputText.Text);
    
  6. Press F5 to run the program.

  7. Type multiple lines of text in the text box, and then click Retrieve.

  8. Verify that the message displays all the text that you typed in the text box.

See Also

Concepts

Designing a User Interface in Visual C#

Other Resources

Text Controls

Visual C# Guided Tour