Control.Text 属性

获取或设置与此控件关联的文本。

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

语法

声明
<LocalizableAttribute(True)> _
<BindableAttribute(True)> _
Public Overridable Property Text As String
用法
Dim instance As Control
Dim value As String

value = instance.Text

instance.Text = value
[LocalizableAttribute(true)] 
[BindableAttribute(true)] 
public virtual string Text { get; set; }
[LocalizableAttribute(true)] 
[BindableAttribute(true)] 
public:
virtual property String^ Text {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_Text ()

/** @property */
public void set_Text (String value)
public function get Text () : String

public function set Text (value : String)

属性值

与该控件关联的文本。

备注

每个派生类以不同的方式使用控件的 Text 属性。例如,FormText 属性在窗体顶部的标题栏中显示,在字符统计中相当少,且通常显示应用程序或文档名称。但是,RichTextBoxText 属性可以很大,且可以包括许多用于设置文本格式的不可见字符。例如,可以通过调整 Font 属性,或通过添加空格或制表符来将文本对齐,以设置 RichTextBox 中显示的文本的格式。

给继承者的说明 在派生类中重写 Text 属性时,请使用基类的 Text 属性来扩展基实现。否则,您必须提供所有实现。不需要同时重写 Text 属性的 getset 访问器;如果需要,可以只重写其中一个访问器。

示例

下面的代码示例创建 GroupBox 并设置该控件的一些通用属性。该示例创建一个 TextBox 并设置它在分组框中的 Location。接着,它设置分组框的 Text 属性,并将此分组框停靠在窗体的顶部。最后,它通过将 Enabled 属性设置为 false 来禁用分组框,这将导致分组框中的所有控件都被禁用。

' Add a GroupBox to a form and set some of its common properties.
Private Sub AddMyGroupBox()
   ' Create a GroupBox and add a TextBox to it.
   Dim groupBox1 As New GroupBox()
   Dim textBox1 As New TextBox()
   textBox1.Location = New Point(15, 15)
   groupBox1.Controls.Add(textBox1)
   
   ' Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox"
   groupBox1.Dock = DockStyle.Top
   
   ' Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = False
   
   ' Add the Groupbox to the form.
   Me.Controls.Add(groupBox1)
End Sub
// Add a GroupBox to a form and set some of its common properties.
private void AddMyGroupBox()
{
   // Create a GroupBox and add a TextBox to it.
   GroupBox groupBox1 = new GroupBox();
   TextBox textBox1 = new TextBox();
   textBox1.Location = new Point(15, 15);
   groupBox1.Controls.Add(textBox1);

   // Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox";
   groupBox1.Dock = DockStyle.Top;

   // Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = false;

   // Add the Groupbox to the form.
   this.Controls.Add(groupBox1);
}
   // Add a GroupBox to a form and set some of its common properties.
private:
   void AddMyGroupBox()
   {
      // Create a GroupBox and add a TextBox to it.
      GroupBox^ groupBox1 = gcnew GroupBox;
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Location = Point(15,15);
      groupBox1->Controls->Add( textBox1 );

      // Set the Text and Dock properties of the GroupBox.
      groupBox1->Text = "MyGroupBox";
      groupBox1->Dock = DockStyle::Top;

      // Disable the GroupBox (which disables all its child controls)
      groupBox1->Enabled = false;

      // Add the Groupbox to the form.
      this->Controls->Add( groupBox1 );
   }
// Add a GroupBox to a form and set some of its common properties.
private void AddMyGroupBox()
{
    // Create a GroupBox and add a TextBox to it.
    GroupBox groupBox1 = new GroupBox();
    TextBox textBox1 = new TextBox();
    textBox1.set_Location(new Point(15, 15));
    groupBox1.get_Controls().Add(textBox1);
    // Set the Text and Dock properties of the GroupBox.
    groupBox1.set_Text("MyGroupBox");
    groupBox1.set_Dock(DockStyle.Top);
    // Disable the GroupBox (which disables all its child controls)
    groupBox1.set_Enabled(false);
    // Add the Groupbox to the form.
    this.get_Controls().Add(groupBox1);
} //AddMyGroupBox

平台

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

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
TextBoxBase.TextLength