TextBox.TextAlign 属性

获取或设置 TextBox 控件中文本的对齐方式。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Property TextAlign As HorizontalAlignment
用法
Dim instance As TextBox
Dim value As HorizontalAlignment

value = instance.TextAlign

instance.TextAlign = value
[LocalizableAttribute(true)] 
public HorizontalAlignment TextAlign { get; set; }
[LocalizableAttribute(true)] 
public:
property HorizontalAlignment TextAlign {
    HorizontalAlignment get ();
    void set (HorizontalAlignment value);
}
/** @property */
public HorizontalAlignment get_TextAlign ()

/** @property */
public void set_TextAlign (HorizontalAlignment value)
public function get TextAlign () : HorizontalAlignment

public function set TextAlign (value : HorizontalAlignment)

属性值

HorizontalAlignment 枚举值之一,指定控件中文本的对齐方式。默认为 HorizontalAlignment.Left

异常

异常类型 条件

InvalidEnumArgumentException

分配给该属性的值不在该枚举的有效值范围内。

备注

可以使用该属性使 TextBox 内的文本对齐,以便和窗体上文本的布局方式相匹配。例如,如果控件都位于窗体的右侧,则可以将 TextAlign 属性设置为 HorizontalAlignment.Right,这样文本将与控件的右侧对齐,而不是默认的左对齐方式。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 在基于 Pocket PC 的应用程序中,单行文本框只支持左对齐方式。多行文本框可向左对齐、向右对齐或居中对齐。

示例

下面的代码示例创建用于接受密码的 TextBox 控件。此示例使用 CharacterCasing 属性将键入的所有字符更改为大写字符,并使用 MaxLength 属性将密码长度限制为八个字符。此示例还使用 TextAlign 属性使密码在 TextBox 控件中居中显示。

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
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:
   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.set_MaxLength(8);
    // Assign the asterisk to be the password character.
    textBox1.set_PasswordChar('*');
    // Change all text entered to be lowercase.
    textBox1.set_CharacterCasing(CharacterCasing.Lower);
    // Align the text in the center of the TextBox control.
    textBox1.set_TextAlign(HorizontalAlignment.Center);
} //CreateMyPasswordTextBox
public function CreateMyPasswordTextBox()
 {
    // Create an instance of the TextBox control.
    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;
 }
 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

TextBox 类
TextBox 成员
System.Windows.Forms 命名空间
HorizontalAlignment 枚举