共用方式為


TextBox.TextAlign 屬性

定義

取得或設定在 TextBox 控制項中文字的對齊方式。

public:
 property System::Windows::Forms::HorizontalAlignment TextAlign { System::Windows::Forms::HorizontalAlignment get(); void set(System::Windows::Forms::HorizontalAlignment value); };
public System.Windows.Forms.HorizontalAlignment TextAlign { get; set; }
member this.TextAlign : System.Windows.Forms.HorizontalAlignment with get, set
Public Property TextAlign As HorizontalAlignment

屬性值

HorizontalAlignment

其中一個 HorizontalAlignment 列舉值,指出在控制項中文字的對齊方式。 預設為 HorizontalAlignment.Left

例外狀況

不在列舉型別 (Enumeration) 有效值範圍內的值會指派給這個屬性。

範例

下列程式碼範例會 TextBox 建立用來接受密碼的 控制項。 這個範例會 CharacterCasing 使用 屬性,將所有輸入為小寫的字元變更,而 MaxLength 屬性則會將密碼長度限制為八個字元。 這個範例也會使用 TextAlign 屬性將 控制項中的密碼置中 TextBox

public:
   void CreateMyPasswordTextBox()
   {
      // Create an instance of the TextBox control.
      TextBox^ textBox1 = gcnew TextBox;
      // Set the maximum length of text in the control to eight.
      textBox1->MaxLength = 8;
      // Assign the asterisk to be the password character.
      textBox1->PasswordChar = '*';
      // Change all text entered to be lowercase.
      textBox1->CharacterCasing = CharacterCasing::Lower;
      // Align the text in the center of the TextBox control.
      textBox1->TextAlign = HorizontalAlignment::Center;
   }
public void CreateMyPasswordTextBox()
 {
    // Create an instance of the TextBox control.
    TextBox textBox1 = new TextBox();
    // Set the maximum length of text in the control to eight.
    textBox1.MaxLength = 8;
    // Assign the asterisk to be the password character.
    textBox1.PasswordChar = '*';
    // Change all text entered to be lowercase.
    textBox1.CharacterCasing = CharacterCasing.Lower;
    // Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center;
 }
Public Sub CreateMyPasswordTextBox()
    ' Create an instance of the TextBox control.
    Dim textBox1 As New TextBox()
    ' Set the maximum length of text in the control to eight.
    textBox1.MaxLength = 8
    ' Assign the asterisk to be the password character.
    textBox1.PasswordChar = "*"c
    ' Change all text entered to be lowercase.
    textBox1.CharacterCasing = CharacterCasing.Lower
    ' Align the text in the center of the TextBox control.
    textBox1.TextAlign = HorizontalAlignment.Center
End Sub

備註

您可以使用這個屬性來對齊 內的 TextBox 文字,以符合表單上的文字配置。 例如,如果您的控制項全都位於表單右側,您可以將 屬性 HorizontalAlignment.Right 設定 TextAlign 為 ,而文字會與控制項右側對齊,而不是預設的左對齊方式。

適用於

另請參閱