TextBox 类

表示 Windows 文本框控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class TextBox
    Inherits TextBoxBase
用法
Dim instance As TextBox
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class TextBox : TextBoxBase
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class TextBox : public TextBoxBase
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class TextBox extends TextBoxBase
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class TextBox extends TextBoxBase

备注

使用 TextBox 控件,用户可以在应用程序中输入文本。此控件具有标准 Windows 文本框控件所没有的附加功能,包括多行编辑和密码字符屏蔽。

通常,TextBox 控件用于显示单行文本或将单行文本作为输入来接受。可以使用 MultilineScrollBars 属性,从而能够显示或输入多行文本。通过将 AcceptsTabAcceptsReturn 属性设置为 true,可在多行 TextBox 控件中更加灵活地操作文本。

通过将 MaxLength 属性设置为一个特定的字符数,可以限制输入到 TextBox 控件中的文本数量。TextBox 控件还可用于接受密码和其他敏感信息。可以使用 PasswordChar 属性屏蔽在控件的单行版本中输入的字符。使用 CharacterCasing 属性可使用户在 TextBox 控件中只能输入大写字符、只能输入小写字符,或者输入大小写字符的组合。

若要限制某些文本不被输入到 TextBox 控件,可以为 KeyDown 事件创建一个事件处理程序,以便验证在控件中输入的每个字符。也可以通过将 ReadOnly 属性设置为 true 来限制 TextBox 控件中的所有数据项输入。

提示

TextBox 控件的大多数功能是从 TextBoxBase 类继承的。

提示

在启用视觉样式时使用 TextBox 控件会导致不能正确地处理代理项字体。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 在 Pocket PC 应用程序中,单行文本框中的选项卡显示为括号,但当 Multiline 设置为 true 时正常显示。

示例

下面的代码示例创建带垂直滚动条的多行 TextBox 控件。此示例使用 AcceptsTabAcceptsReturnWordWrap 属性来使多行文本框控件可用于创建文本文档。

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.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!";
 }
 
private:
   void CreateMyMultilineTextBox()
   {
      // Create an instance of a TextBox control
      TextBox^ textBox1 = gcnew 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!";
   }
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!";
 }
 

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TextBox 成员
System.Windows.Forms 命名空间
TextBoxBase