Control.Text 属性

定义

获取或设置与此控件关联的文本。Gets or sets the text associated with this control.

public:
 virtual property System::String ^ Text { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public virtual string Text { get; set; }
member this.Text : string with get, set
Public Overridable Property Text As String

属性值

与该控件关联的文本。The text associated with this control.

属性

示例

下面的代码示例创建 GroupBox,并设置其一些公共属性。The following code example creates a GroupBox and sets some of its common properties. 该示例创建一个 TextBox 并在 "分组" 框中设置其 LocationThe example creates a TextBox and sets its Location within the group box. 接下来,它设置分组框的 Text 属性,并将组框停靠到窗体的顶部。Next, it sets the Text property of the group box, and docks the group box to the top of the form. 最后,它通过将 Enabled 属性设置为 false 来禁用分组框,这将导致禁用组中包含的所有控件。Lastly, it disables the group box by setting the Enabled property to false, which causes all controls contained within the group box to be disabled.

   // 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.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 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

注解

每个派生类以不同方式使用控件的 Text 属性。The Text property of the control is used differently by each derived class. 例如,在窗体顶部的标题栏中显示 FormText 属性,在字符计数中相当小,通常显示应用程序或文档名称。For example the Text property of a Form is displayed in the title bar at the top of the form, is fairly small in character count, and usually displays the application or document name. 不过,RichTextBoxText 属性可能会很大,并且可以包含用于设置文本格式的大量非可视字符。However, the Text property of a RichTextBox can be large and can include numerous nonvisual characters used to format the text. 例如,可以通过调整 Font 属性或通过添加空格或制表符来对齐文本来设置 RichTextBox 中显示的文本的格式。For example, the text displayed in a RichTextBox can be formatted by adjusting the Font properties, or by the addition of spaces or tab characters to align the text.

继承者说明

在派生类中重写 Text 属性时,请使用基类的 Text 属性来扩展基实现。When overriding the Text property in a derived class, use the base class's Text property to extend the base implementation. 否则,必须提供所有实现。Otherwise, you must provide all the implementation. 不需要重写 Text 属性的 getset 访问器;如果需要,只能重写一个。You are not required to override both the get and set accessors of the Text property; you can override only one if needed.

适用于

另请参阅