Microsoft KB Archive/873197

= The text in right-aligned label controls is not right-aligned when the TextAlign property is set to MiddleRight =

Article ID: 873197

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



SYMPTOMS
When you vertically align label controls and set the TextAlign property of the label controls to MiddleRight, the text in all the label controls may not be right-aligned.



CAUSE
The Microsoft .NET Framework uses the Microsoft Windows GDI+ version instead of the GDI version. A GDI+ text layout is resolution independent. The text in a Windows form uses GDI+. Therefore, the text is not right-aligned in the label controls on Windows forms.



RESOLUTION
To resolve this problem, add a newline character or change the properties of the label controls. To add a newline character, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to Open, and then click Project. Locate the project, and then click Open. On the View menu, click Solution Explorer. In Solution Explorer, expand  (where   is the name of the project that you created). Right-click the Form1.cs file or the Form1.vb file, and then click View Code.

Note If you are using Microsoft Visual C# .NET, the file is a Form1.cs file. If you are using Microsoft Visual Basic .NET, the file is a Form1.vb file.  For Visual Basic .NET, locate the following code: Me.Label3.Text = &quot;!!!!!!!!&quot; Replace the previous code with the following code: Me.Label3.Text = &quot;!!!!!!!!&quot; + vbCrLf For Visual C# .NET, locate the following code: this.label3.Text = &quot;!!!!!!!!&quot;; Replace the previous code with one of the following code samples: this.label3.Text=&quot;!!!!!!!!&quot; + &quot;\n&quot;; this.label3.Text=&quot;!!!!!!!!&quot; + &quot;\r&quot;; </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.

Note All the text in the label controls may be right-aligned.</li></ol>

To change the properties of the label controls, follow these steps:
 * 1) Start Visual Studio .NET.
 * 2) On the File menu, point to Open, and then click Project.
 * 3) Locate the project, and then click Open.
 * 4) On the View menu, click Solution Explorer.
 * 5) In Solution Explorer, expand   (where   is the name of the project that you created).
 * 6) Right-click the Form1.cs file or the Form1.vb file, and then click View Designer.
 * 7) In design view for the Form1.cs file or the Form1.vb file, right-click the label control that you want to change, and then click Properties.
 * 8) In the Properties window, set the TextAlign property to MiddleLeft, and then set the RightToLeft property to Yes.
 * 9) On the Build menu, click Build Solution.
 * 10) On the Debug menu, click Start.

Note All the text in the label controls may be right-aligned.

<div class="workaround_section">

WORKAROUND
To work around this problem, use text box controls instead of label controls. You can also create a control that does not use GDI+.

<div class="moreinformation_section">

Steps to reproduce the problem

 * 1) Start Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual Basic Projects or Visual C# Projects.
 * 4) Under Templates, click Windows Application.
 * 5) In the Name box, type the name of the project, and then click OK.

By default, a form that is named Form1 is created.
 * 1) Add three label controls to the Form1 form so that they have the same vertical alignment.
 * 2) Right-click the Label1 label control, and then click Properties.
 * 3) In the Properties window, locate and then set the following properties:
 * 4) * Set the BorderStyle property to FixedSingle.
 * 5) * Set the Font property to Microsoft Sans Serif, 12pt.
 * 6) * Set the TextAlign property to MiddleRight.
 * 7) Repeat step 7 and step 8 for the Label2 label control and the Label3 label control.
 * 8) Use the following information to set the property values:
 * 9) * Set the Text property of the Label1 label control to hay.
 * 10) * Set the Text property of the Label2 label control to jeff.
 * 11) * Set the Text property of the Label3 label control to !!!!!!!!.
 * 12) On the Build menu, click Build Solution.
 * 13) On the Debug menu, click Start.

The right character of the Label3 label control is not vertically aligned with the text in the other two label controls.

<div class="references_section">