TextBox Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
'Declaration <ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class TextBox Inherits TextBoxBase 'Usage Dim instance As TextBox
/** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ public class TextBox extends TextBoxBase
ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class TextBox extends TextBoxBase
With the TextBox control, the user can enter text in an application. This control has additional functionality that is not found in the standard Windows text box control, including multiline editing and password character masking.
Typically, a TextBox control is used to display, or accept as input, a single line of text. You can use the Multiline and ScrollBars properties to enable multiple lines of text to be displayed or entered. Set the AcceptsTab and AcceptsReturn properties to true to enable greater text manipulation in a multiline TextBox control.
You 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. You 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 restrict text from being entered in a TextBox control, you can create an event handler for the KeyDown event in order to validate each character entered in the control. You can also restrict all entry of data in a TextBox control by setting the ReadOnly property to true.
Note |
|---|
| Most of the functionality of the TextBox control is inherited from the TextBoxBase class. |
Note |
|---|
| Using the TextBox control with visual styles enabled will cause the incorrect handling of surrogate fonts. |
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: In Pocket PC applications, tabs in a single-line text box display as brackets, but display in the usual way when Multiline is set to true.
The following code example creates a multiline TextBox control with vertical scroll bars. This example uses the AcceptsTab, AcceptsReturn, and WordWrap properties to make the multiline text box control useful for creating text documents.
Private Sub CreateMyMultilineTextBox() ' Create an instance of a TextBox control. Dim textBox1 As New TextBox() ' Set the Multiline property to true. textBox1.Multiline = True ' Add vertical scroll bars to the TextBox control. textBox1.ScrollBars = ScrollBars.Vertical ' Allow the RETURN key to be entered in the TextBox control. textBox1.AcceptsReturn = True ' Allow the TAB key to be entered in the TextBox control. textBox1.AcceptsTab = True ' Set WordWrap to True to allow text to wrap to the next line. textBox1.WordWrap = True ' Set the default text of the control. textBox1.Text = "Welcome!" End Sub
private void CreateMyMultilineTextBox()
{
// Create an instance of a TextBox control.
TextBox textBox1 = new TextBox();
// Set the Multiline property to true.
textBox1.set_Multiline(true);
// Add vertical scroll bars to the TextBox control.
textBox1.set_ScrollBars(ScrollBars.Vertical);
// Allow the RETURN key to be entered in the TextBox control.
textBox1.set_AcceptsReturn(true);
// Allow the TAB key to be entered in the TextBox control.
textBox1.set_AcceptsTab(true);
// Set WordWrap to True to allow text to wrap to the next line.
textBox1.set_WordWrap(true);
// Set the default text of the control.
textBox1.set_Text("Welcome!");
} //CreateMyMultilineTextBox
private function CreateMyMultilineTextBox() { // Create an instance of a TextBox control textBox1 = new TextBox(); // Set the Multiline property to true. textBox1.Multiline = true; // Add vertical scroll bars to the TextBox control. textBox1.ScrollBars = ScrollBars.Vertical; // Allow the RETURN key to be entered in the TextBox control. textBox1.AcceptsReturn = true; // Allow the TAB key to be entered in the TextBox control. textBox1.AcceptsTab = true; // Set WordWrap to True to allow text to wrap to the next line. textBox1.WordWrap = true; // Set the default text of the control. textBox1.Text = "Welcome!"; }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note