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
On the File menu, click NewProject.
In the New Project dialog box, click Windows Forms Application, and then click OK.
A new Windows Forms project opens.
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
Add a Button control next to the text box, and change the following properties:
Property
Value
Name
retrieveInput
Text
Retrieve
Double-click the button to create the retrieveInput_Click event handler, and add the following code:
MessageBox.Show(this.inputText.Text);
Press F5 to run the program.
Type multiple lines of text in the text box, and then click Retrieve.
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#