TextBox

Used to take any user input in the application.
TextBox
API: System.Windows.Forms.TextBox
Naming Convension: txtxxxxxx

Properties of TextBox

Property Description
Name Specifies the name of the control.
Text Specifies the displayable text of the control.
BackColor Specifies the background color of the control.
ForeColor Specifies the foreground color of the control.
Font Specifies the font style of the control’s text.
Enabled Enables / Disables the control.
Visible Displays / Hides the control.
Cursor Specifies the mouse pointer style, when it is over on the control.
Size Specifies the Width and Height of the control.
Location Specifies the X and Y co-ordinations of the control’s position on the form.
TextAlign Specifies the position of the text in the control.
Image Specifies the image that is to be displayed in the control along with the text.
ImageAlign Specifies the position of the image in the control
TabIndex Specifies the index of the control in the tab order.
ContextMenuStrip Contains the reference of the respective context menu control.
ReadOnly Enables / disables read-only nature of the textbox. In the read only textbox, the user can not enter any text.
MultiLine Enables / disables multiple lines in the text box. By default, the text box will be single-line textbox.
WordWrap This is used in multi line textboxes, which automatically moves the cursor to the next line, when the current line exceeds.
Scrollbars Enables / disables scroll bars in the textbox.
PasswordChar Used to specify the password display character. Ex: *
MaxLength Specifies the maximum no. of characters, that can be entered in the textbox.

Events of TextBox
<
Event Description
TextChanged Executes when any character is typed / removed in the textbox.
Click Executes when the user clicks the control run time.
DoubleClick Executes when the user double-clicks the control at run time.
MouseMove Executes when the mouse pointer is moves across the control.
MouseEnter Executes when the mouse pointer is focused on to the control.
MouseLeave Executes when the mouse pointer is out of the button.
KeyPress Executes when any key is pressed on the keyboard, while the focus is on the control.
Enter Executes when the focus is got on to the control.
Leave Executes when the focus is out of the control.

Methods of TextBox
Method Description
Clear() Clears all the contents of the textbox and makes it empty.
Focus() Moves the focus to the control.

Demo on TextBox

TextBox
Design
label1:
Text: Enter your Name:
Name: lblName

textBox1:
Name: txtName
label2:
Text: Message
Name: lblMessage
Visible: false

button1:
Name: btnOK
Text: OK
ForeColor: Red


private void btnOK_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string message = "Welcome to " + name;
lblMessage.Text = message;
lblMessage.Visible = true;
}

Demo on TextBox

TextBox
Design
label1:
Text: Enter First Value:
Name: lblFirstValue

textBox1:
Name: txtFirstValue

label2:
Text: Enter Second Value:
Name: lblSecondValue

textBox2:
Name: txtSecondValue

button1:
Name: btnAdd
Text: +

button2:
Name: btnSubtract
Text: -

button3:
Name: btnMultiply
Text: *

button4:
Name: btnDivide
Text: /

label3:
Text: Result:
Name: lblResult

textBox3:
Name: txtResult
ReadOnly: true


private void btnAdd_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a + b;
txtResult.Text = Convert.ToString(c);
}

private void btnSubtract_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a - b;
txtResult.Text = Convert.ToString(c);
}

private void btnMultiply_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a * b;
txtResult.Text = Convert.ToString(c);
}
private void btnDivide_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirstValue.Text);
int b = Convert.ToInt32(txtSecondValue.Text);
int c = a / b;
txtResult.Text = Convert.ToString(c);
}

Demo on TextBox

TextBox
Design
label1:
Text: Enter your text here:
Name: lblSourceText

textBox1:
Name: txtSourceText

label2:
Text: Copied Text:
Name: lblDestinationText

textBox2:
Name: txtDestinationText


private void txtSourceText_TextChanged(object sender, EventArgs e)
{
txtDestinationText.Text = txtSourceText.Text;
}

Demo on TextBox

TextBox
Design
label1:
Text: Generate Numbers
Name: lblGenerateNumbers

label2:
Text: From:
Name: lblFrom

textBox1:
Name: txtFrom

label3:
Text: To:
Name: lblTo

textBox2:
Name: txtTo

button1:
Text: GO:
Name: btnGO

textBox3:
Name: txtNumbers
Multiline: true
Readonly: true
Scrollbars: Vertical


private void btnGO_Click(object sender, EventArgs e)
{
int n1 = Convert.ToInt32(txtFrom.Text);
int n2 = Convert.ToInt32(txtTo.Text);
txtNumbers.Clear();
for (int i = n1; i <= n2; i++)
{
txtNumbers.Text = txtNumbers.Text + i + ", ";
}
txtFrom.Focus();
}