Export (0) Print
Expand All

StringFormat.Alignment Property

Gets or sets text alignment information on the vertical plane.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

public StringAlignment Alignment { get; set; }
/** @property */
public StringAlignment get_Alignment ()

/** @property */
public void set_Alignment (StringAlignment value)

public function get Alignment () : StringAlignment

public function set Alignment (value : StringAlignment)

Not applicable.

Property Value

A StringAlignment enumeration that specifies text alignment information.

The following code example demonstrates how to use the LineAlignment and Alignment properties and the StringAlignment enumeration to align strings. This example is designed to be used with Windows Forms. Paste the code into a form and call the ShowLineAndAlignment method when handling the form's Paint event, passing e as PaintEventArgs.

private void ShowLineAndAlignment(PaintEventArgs e)
{

    // Construct a new Rectangle .
    Rectangle  displayRectangle = 
        new Rectangle (new Point(40, 40), new Size (80, 80));

    // Construct 2 new StringFormat objects
    StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
    StringFormat format2 = new StringFormat(format1);

    // Set the LineAlignment and Alignment properties for
    // both StringFormat objects to different values.
    format1.LineAlignment = StringAlignment.Near;
    format1.Alignment = StringAlignment.Center;
    format2.LineAlignment = StringAlignment.Center;
    format2.Alignment = StringAlignment.Far;

    // Draw the bounding rectangle and a string for each
    // StringFormat object.
    e.Graphics.DrawRectangle(Pens.Black, displayRectangle);
    e.Graphics.DrawString("Showing Format1", this.Font, 
        Brushes.Red, (RectangleF)displayRectangle, format1);
    e.Graphics.DrawString("Showing Format2", this.Font, 
        Brushes.Red, (RectangleF)displayRectangle, format2);
}

private void ShowLineAndAlignment(PaintEventArgs e)
{
    // Construct a new Rectangle .
    Rectangle displayRectangle = new Rectangle(new Point(40, 40),
                                new Size(80, 80));

    // Construct 2 new StringFormat objects
    StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
    StringFormat format2 = new StringFormat(format1);

    // Set the LineAlignment and Alignment properties for
    // both StringFormat objects to different values.
    format1.set_LineAlignment(StringAlignment.Near);
    format1.set_Alignment(StringAlignment.Center);
    format2.set_LineAlignment(StringAlignment.Center);
    format2.set_Alignment(StringAlignment.Far);

    // Draw the bounding rectangle and a string for each
    // StringFormat object.
    e.get_Graphics().DrawRectangle(Pens.get_Black(), displayRectangle);
    e.get_Graphics().DrawString("Showing Format1", this.get_Font(), 
        Brushes.get_Red(), (RectangleF.op_Implicit((displayRectangle))),
        format1);
    e.get_Graphics().DrawString("Showing Format2", this.get_Font(),
        Brushes.get_Red(), RectangleF.op_Implicit((displayRectangle)),
        format2);
} //ShowLineAndAlignment

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

Community Additions

ADD
Show:
© 2014 Microsoft