TextBox.TextAlign 屬性

定義

取得或設定在 TextBox 控制項中文字的對齊方式。Gets or sets how text is aligned in a TextBox control.

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 列舉值,指出在控制項中文字的對齊方式。One of the HorizontalAlignment enumeration values that specifies how text is aligned in the control. 預設為 HorizontalAlignment.LeftThe default is HorizontalAlignment.Left.

例外狀況

不在列舉型別 (Enumeration) 有效值範圍內的值會指派給這個屬性。A value that is not within the range of valid values for the enumeration was assigned to the property.

範例

下列程式碼範例會建立 TextBox 用來接受密碼的控制項。The following code example creates a TextBox control that is used to accept a password. 此範例使用 CharacterCasing 屬性來變更所有輸入為小寫的字元,以及將 MaxLength 密碼長度限制為八個字元的屬性。This example uses the CharacterCasing property to change all characters typed to lowercase, and the MaxLength property to restrict the password length to eight characters. 這個範例也會使用 TextAlign 屬性,將密碼置中在 TextBox 控制項中。This example also uses the TextAlign property to center the password in the TextBox control.

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 ,以符合表單上文字的版面配置。You can use this property to align the text within a TextBox to match the layout of text on your form. 例如,如果您的控制項都位於表單的右邊,您可以將 TextAlign 屬性設定為 HorizontalAlignment.Right ,而且文字會與控制項的右邊對齊,而不是預設的靠左對齊。For example, if your controls are all located on the right side of the form, you can set the TextAlign property to HorizontalAlignment.Right, and the text will be aligned with the right side of the control instead of the default left alignment.

適用於

另請參閱