The TextBox control (System.Windows.Forms.TextBox) is the most basic means of input in a windows forms. You give the input data by typing it inside the text box. The text you type can be accessed by using the Text property of the control. The following table shows some useful properties that you can use for the TextBox control.

Property Description
AcceptsReturn Used with Multiline. Tells if whether the return key is included in the input. The return will be converted into a
escape sequence.
AcceptsTab Indicates whether to accept tab as an input. By default, hitting tab will bring the focus to the control with the next TabIndex in the form.
Enabled Set to false to make the text box read-only therefore making the text box act like a label.
Font The font properties that will be used by the textbox.
Lines The lines of text in a multiline text box.
Multiline Set to true to allow multiple lines in a text box.
Text The text inside the text box.
PasswordChar Accepts a character that will be used to mask each character typed by the user.
ReadOnly Tells whether the text in the form can be edited.
Visible Tells whether the text box is visible inside the form.
WordWrap Used with Multiline. Set to true to enable automatic wrapping of words.

Figure 1

The following example shows the use of text boxes. Create a new Windows Application project. The program will ask two numbers and then when a button is pressed, the sum is shown using a label. Drag 2 textboxes to the shape and name them textBoxFirstNumberand textBoxSecondNumber. additionally drag one label for every text box indicating their purpose. Drag another label which will be wont to show the add and name it labelSum. Place a Button management and name it buttonAdd. modify the position and sizes of the controls to match the layout below.

Double-click the button to add an event handler to its click event. Type the following codes.

private void buttonAdd_Click(object sender, EventArgs e)
{
    int num1 = Int32.Parse(textBoxFirstNumber.Text);
    int num2 = Int32.Parse(textBoxSecondNumber.Text);
    int sum = num1 + num2;

    labelSum.Text = "Sum = " + sum;
}

The code converts the contents of textBoxFirstNumber and textBoxSecondNumber into integers exploitation the Parse method of the Int32class and hold on them in their several variables.We require conversion because the Text property is of type string. Their contents are accessed using the Text property. The sum is then determined. The sum is displayed afterward by assigning it to the Text property of labelSum.

The most useful event of the TextBox is the TextChanged event which occurs when the text inside the textbox is modified. The following program shows an example of using this event.

Create another windows forms application and add a textbox and a label in the form. Name them you can leave their default names right now.

Delete the text of label1 by removing the content of its Text property. Note that it would be hard to select this label now if the AutoSize is set to false so you can select this label using the top combo box of the Properties Window. The default event of the TextBoxcontrol is the TextChanged event so double clicking it will create an event handler for the said event. Add the following code to the event handler.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    label1.Text = textBox1.Text;
}

Now run the program and type anything in the text box. Notice that the text of the label copies the text you type in the text box.

When the text inside the textbox is modified, the event handler executes and copies the text inside textbox1 to label1.

By default, a text box can only handle a single line of text. To make it a multiline textbox, simply set the Multiline property to true. Once you do that, you will notice that you can change the height of the textbox in the designer.

You can set the WordWrap property to true to automatically bring the cursor below when it reaches the right edge of the text box. If it is set to false, the text will continue to the to the right clipping the ones at the left side. You can set the ScrollBar property to HorizontalVertical or Both to add scroll bars to a multiline text box. When using a multiline textbox, you can use the Lines property to retrieve the individual lines of text.

If your textbox is expected to accept passwords, then you must set the PasswordChar to something like * to mask each character type in the textbox by the character indicated in the PasswordChar property.