Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

HorizontalAlignment Enumeration

Specifies how an object or text in a control is horizontally aligned relative to an element of the control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ComVisibleAttribute(true)] 
public enum HorizontalAlignment
/** @attribute ComVisibleAttribute(true) */ 
public enum HorizontalAlignment
ComVisibleAttribute(true) 
public enum HorizontalAlignment
 Member nameDescription
Supported by the .NET Compact FrameworkCenterThe object or text is aligned in the center of the control element. 
Supported by the .NET Compact FrameworkLeftThe object or text is aligned on the left of the control element. 
Supported by the .NET Compact FrameworkRightThe object or text is aligned on the right of the control element. 

This enumeration is used in numerous classes. A partial list of these classes is CheckedListBox, ColumnHeader, ComboBox, ControlPaint, Label, ListBox, Control, RichTextBox, and TextBox.

This example shows how to use the HorizontalAlignment enumeration to align the text to the left, right, or in the center of a control element. First, create a TextBox with a specific size, and add a text string to it. Then, use the enumeration member Center to align the text in the center of the TextBox. The example assumes that you have created a Form named Form1 and a TextBox named textBox1.

private void Form1_Load(object sender, System.EventArgs e)
{
    // Add a text string to the TextBox.
    textBox1.Text = "Hello World!";
    
    // Set the size of the TextBox.
    textBox1.AutoSize = false;
    textBox1.Size = new Size(Width, Height/3);
    
    // Align the text in the center of the control element. 
    textBox1.TextAlign = HorizontalAlignment.Center;                            
}

private void Form1_Load(Object sender, System.EventArgs e)
{
    // Add a text string to the TextBox.
    textBox1.set_Text("Hello World!");

    // Set the size of the TextBox.
    textBox1.set_AutoSize(false);
    textBox1.set_Size(new Size(get_Width(), get_Height() / 3));

    // Align the text in the center of the control element. 
    textBox1.set_TextAlign(HorizontalAlignment.Center);
} //Form1_Load

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.