Edit

Share via


HorizontalAlignment Enum

Definition

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

C#
[System.Runtime.InteropServices.ComVisible(true)]
public enum HorizontalAlignment
C#
public enum HorizontalAlignment
Inheritance
HorizontalAlignment
Attributes

Fields

Name Value Description
Left 0

The object or text is aligned on the left of the control element.

Right 1

The object or text is aligned on the right of the control element.

Center 2

The object or text is aligned in the center of the control element.

Examples

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.

C#
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;							
}

Remarks

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

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9