Control.Text Control.Text Control.Text Control.Text Property

定義

取得或設定這個控制項的相關文字。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,並設定其Location群組方塊內。The 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. 比方說Text屬性Form會顯示在表單頂端的標題列中,是相當小的字元數,通常會顯示應用程式或文件的名稱。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. 不過,Text屬性RichTextBox可能很大,而且可以包含多個非視覺化的字元,用來格式化文字。However, the Text property of a RichTextBox can be large and can include numerous nonvisual characters used to format the text. 例如,將中顯示的文字RichTextBox可以藉由調整格式化Font屬性,或加上空格或定位點字元對齊的文字。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. 您不需要覆寫兩個getset存取子Text屬性; 您可以覆寫只能有一個如有需要。You are not required to override both the get and set accessors of the Text property; you can override only one if needed.

適用於

另請參閱