更新:2007 年 11 月
命名空间:
System.Windows.Forms 程序集:
System.Windows.Forms(在 System.Windows.Forms.dll 中)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class TextBox _
Inherits TextBoxBase
[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
public class TextBox extends TextBoxBase
使用 TextBox 控件,用户可以在应用程序中输入文本。此控件具有标准 Windows 文本框控件所没有的附加功能,包括多行编辑和密码字符屏蔽。
通常,TextBox 控件用于显示单行文本或将单行文本作为输入来接受。可以使用 Multiline 和 ScrollBars 属性,从而能够显示或输入多行文本。通过将 AcceptsTab 和 AcceptsReturn 属性设置为 true,可在多行 TextBox 控件中更加灵活地操作文本。
说明: |
|---|
必须将 Multiline 属性设置为 true 以调整 TextBox 控件的高度。通过设置 Size 属性可以调整高度。 |
通过将 MaxLength 属性设置为一个特定的字符数,可以限制输入到 TextBox 控件中的文本数量。TextBox 控件还可用于接受密码和其他敏感信息。可以使用 PasswordChar 属性屏蔽在控件的单行版本中输入的字符。使用 CharacterCasing 属性可使用户在 TextBox 控件中只能输入大写字符、只能输入小写字符,或者输入大小写字符的组合。
若要滚动 TextBox 的内容直到光标(插入符号)位于控件的可见区域内,可以使用 ScrollToCaret 方法。若要选择文本框中的文本范围,可以使用 Select 方法。
若要限制某些文本不被输入到 TextBox 控件,可以为 KeyDown 事件创建一个事件处理程序,以便验证在控件中输入的每个字符。也可以通过将 ReadOnly 属性设置为 true 来限制 TextBox 控件中的所有数据项输入。
说明: |
|---|
TextBox 控件的大多数功能是从 TextBoxBase 类继承的。 在启用视觉样式时使用 TextBox 控件会导致不能正确地处理代理项字体。 |
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明:
在 Pocket PC 应用程序中,单行文本框中的选项卡显示为括号,但当 Multiline 设置为 true 时正常显示。
下面的代码示例创建带垂直滚动条的多行 TextBox 控件。此示例使用 AcceptsTab、AcceptsReturn 和 Dock 属性来使多行文本框控件可用于创建文本文档。
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Dim textBox1 As TextBox
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeComponent()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
' textBox1
'
Me.textBox1.AcceptsReturn = True
Me.textBox1.AcceptsTab = True
Me.textBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.textBox1.Multiline = True
Me.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
'
' Form1
'
Me.ClientSize = New System.Drawing.Size(284, 264)
Me.Controls.Add(Me.textBox1)
Me.Text = "TextBox Example"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
End Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private TextBox textBox1;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.AcceptsTab = true;
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Multiline = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.textBox1);
this.Text = "TextBox Example";
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
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
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:3.5、2.0、1.0
参考