Friday, October 7, 2011
TextBox Control in C#
Introduction
Windows Forms text boxes are used to get input from the user or to display text. The TextBox control is generally used for editable text, although it can also be made read-only. Text boxes can display multiple lines, wrap text to the size of the control, and add basic formatting. The TextBox control allows a single format for text displayed or entered in the control.To display multiple types of formatted text, use the RichTextBox control.The text displayed by the control is contained in the Text property. By default, you can enter up to 2048 characters in a text box. If you set the MultiLine property to true, you can enter up to 32 KB of text.
The code below sets text in the control at run time.
The InitializeMyControl procedure will not execute automatically it must be called.
Code: CPP
private void InitializeMyControl()
{
// Put some text into the control first.
textBox1.Text = "This is a TextBox control.";
}
Insertion point in a TextBox control
When a Windows Forms TextBox control first receives the focus, the default insertion within the text box is to the left of any existing text. The user can move the insertion point with the keyboard or the mouse. If the text box loses and then regains the focus, the insertion point will be wherever the user last placed it.
To control the insertion point in a TextBox control
1. Set the SelectionStart property to an appropriate value. Zero places the insertion point immediately to the left of the first character.
2. Set the SelectionLength property to the length of the text you want to select.
Code: CPP
private void textBox1_Enter(Object sender, System.EventArgs e)
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
}
To make the text box insertion point visible by default on a new form Set the TextBox control's TabIndex property to 0.
A password box is a Windows Forms text box that displays placeholder characters while a user types a string.
To create a password text box
We have to Set the PasswordChar property of the TextBox control to a specific character.
The PasswordChar property specifies the character displayed in the text box. For example, if you want asterisks displayed in the password box, specify * for the PasswordChar property in the Properties window.
Set the MaxLength property. The property determines how many characters can be typed in the text box. If the maximum length is exceeded, the system emits a beep and the text box does not accept any more characters. Note that you may not wish to do this as the maximum length of a password may be of use to hackers who are trying to guess the password.
The code below initializes a text box that will accept a string up to 14 characters long and display asterisks in place of the string. Using the PasswordChar property on a text box can help ensure that other people will not be able to determine a user's password if they observe the user entering it.
Code: CPP
private void InitializeMyControl()
{
// Set to no text.
textBox1.Text = "";
// The password character is an asterisk.
textBox1.PasswordChar = '*';
// The control will allow no more than 14 characters.
textBox1.MaxLength = 14;
}
Multiple lines in the TextBox control
By default, the Windows Forms TextBox control displays a single line of text and does not display scroll bars. If the text is longer than the available space, only part of the text is visible. You can change this default behavior by setting the MultiLine, WordWrap, and ScrollBars properties to appropriate values.
To view multiple lines in the TextBox control
1. Set the MultiLine property to true. If WordWrap is true then the text in the control will appear as one or more paragraphs; otherwise it will appear as a list, in which some lines may be clipped at the edge of the control.
2. Set the ScrollBars property to an appropriate value.
None:Use this value if the text will be a paragraph that almost always fits the control. The user can use the mouse pointer to move around inside the control if the text is too long to display all at once.
Horizontal: Use this value if you want to display a list of lines, some of which may be longer than the width of the TextBox control.
Both:Use this value if the list may be longer than the height of the control.
3. Set the WordWrap property to an appropriate value.