Export (0) Print
Expand All
8 out of 44 rated this helpful - Rate this topic

TextBox Class

Represents a Windows text box control.

For a list of all members of this type, see TextBox Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.TextBoxBase
               System.Windows.Forms.TextBox
                  System.Windows.Forms.DataGridTextBox

[Visual Basic]
Public Class TextBox
   Inherits TextBoxBase
[C#]
public class TextBox : TextBoxBase
[C++]
public __gc class TextBox : public TextBoxBase
[JScript]
public class TextBox extends TextBoxBase

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The TextBox control allows the user to 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 allow 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.

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.

.NET Compact Framework Platform Note:  In Pocket PC applications, tabs in a single-line TextBox display as brackets, but display normally when Multiline is set to true.

Example

The following 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.

[Visual Basic] 
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


[C#] 
private void CreateMyMultilineTextBox()
 {
    // Create an instance of a TextBox control.
    TextBox 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!";
 }
 

[C++] 
private:
    void CreateMyMultilineTextBox() {
        // Create an instance of a TextBox control
        TextBox __gc *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 = S"Welcome!";
    };


[JScript] 
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!";
 }
 

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

TextBox Members | System.Windows.Forms Namespace | TextBoxBase

Show:
© 2014 Microsoft. All rights reserved.