Code:
private void btnOK_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello! "+txt1.Text+txt2.Text);
}
A TextBox
control is used to display a single line of text, or accept it as an input. We can use the properties of Multiline and ScrollBars to allow the display or entry of multiple lines of text. In a multiline TextBox control, set the AcceptsTab and AcceptsReturn properties to true to allow greater text manipulation.
The user can enter text in an application using the TextBox control. This control has additional features, including multi-line editing and masking of password characters.
We can limit the amount of text entered into a TextBox control by setting the MaxLength
property to a specific number of characters. TextBox controls can also be used to accept passwords and other sensitive information. We can use the PasswordChar
property to mask characters entered in a single-line version of the control. Use the CharacterCasing
property to enable the user to type only uppercase, only lowercase, or a combination of uppercase and lowercase characters into the TextBox control.
To scroll the contents of the TextBox until the cursor (caret) is within the visible region of the control, we can use the ScrollToCaret method. To select a range of text in the text box, we can use the Select method.
To restrict text from being entered in a TextBox control, we can create an event handler for the KeyDown event in order to validate each character entered in the control. we can also restrict all entry of data in a TextBox control by setting the ReadOnly = true
.
Note: We must set the Multiline property to true to adjust the height of the TextBox control. We can adjust the height by setting the Size property.
We can set background color and foreground color through property window and programmatically.
textbox1.BackColor = Color.Blue;
textbox1.ForeColor = Color.White;
We can set 3 different types of border style for textbox, they are None, FixedSingle and fixed3d.
textbox1.BorderStyle = BorderStyle.Fixed3D;
Keydown event
We can capture which key is pressed by the user using KeyDown
event
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You Press Enter Key");
}
if (e.KeyCode == Keys.CapsLock)
{
MessageBox.Show("You Press Caps Lock Key.");
}
When user input or setting the Text property to a new value raises the TextChanged
event
private void txt1_TextChanged(object sender, EventArgs e)
{
lblWelcomeMessage.Text = txt1.Text;
}
Sets the maximum number of characters or words the user can input into the text box control.
txt1.MaxLength = 40;
If a program wants to prevent a user from changing the text in a text box, the program can set the Read - only
property controls to True
.
txt1.ReadOnly = true;
You can use the properties of Multiline
and ScrollBars
to allow the display or entry of multiple lines of text.
txt1.Multiline = true;
You can also use TextBox controls to accept passwords and other sensitive information. To mask characters entered in a single line version of the control, we can use the PasswordChar
property.
txt1.PasswordChar = '*';
The above code sets the PasswordChar
to *, so it displays only * instead of typed characters when the user enters the password.
We can add new line in a text box using many ways.
txt1.Text += "Our text" + "\r\n";
Or
txt1.Text += "Our Text" + Environment.NewLine;
int i;
i = int.Parse (txt1.Text);
Parse method Converts the string representation of a number to its integer equivalent.
String to Float conversion
float i;
i = float.Parse (txt1.Text);
String to Double conversion
double i;
i = float.Parse (txt1.Text);