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.

例外

分配给该属性的值不在该枚举的有效值范围内。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 属性将密码长度限制为8个字符。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 属性使密码在控件中居中 TextBoxThis 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.

适用于

另请参阅