TextBox.TextAlign 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 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.Left
。
例外
分配给该属性的值不在该枚举的有效值范围内。
示例
下面的代码示例创建一个 TextBox 用于接受密码的控件。 此示例使用 CharacterCasing 该属性将键入的所有字符更改为小写,并将 MaxLength 密码长度限制为 8 个字符的属性。 此示例还使用 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 布局,以匹配窗体上的文本布局。 例如,如果控件都位于窗体的右侧,则可以将 TextAlign 属性设置为该属性 HorizontalAlignment.Right
,并且文本将与控件的右侧对齐,而不是默认的左对齐方式。