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.

適用於

另請參閱