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



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • 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:
  1. Start Microsoft 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 Project (where Project 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 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.
  7. For Visual Basic .NET, locate the following code:
    Me.Label3.Text = "!!!!!!!!"
    Replace the previous code with the following code:
    Me.Label3.Text = "!!!!!!!!" + vbCrLf
    For Visual C# .NET, locate the following code:
    this.label3.Text = "!!!!!!!!";
    Replace the previous code with one of the following code samples:
    this.label3.Text="!!!!!!!!" +  "\n";
    this.label3.Text="!!!!!!!!" +  "\r";
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start.

    Note All the text in the label controls may be right-aligned.
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 Project (where Project 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.

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+.

MORE INFORMATION

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.
  6. Add three label controls to the Form1 form so that they have the same vertical alignment.
  7. Right-click the Label1 label control, and then click Properties.
  8. In the Properties window, locate and then set the following properties:
    • Set the BorderStyle property to FixedSingle.
    • Set the Font property to Microsoft Sans Serif, 12pt.
    • Set the TextAlign property to MiddleRight.
  9. Repeat step 7 and step 8 for the Label2 label control and the Label3 label control.
  10. Use the following information to set the property values:
    • Set the Text property of the Label1 label control to hay.
    • Set the Text property of the Label2 label control to jeff.
    • Set the Text property of the Label3 label control to !!!!!!!!.
  11. On the Build menu, click Build Solution.
  12. 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.

REFERENCES

For more information, visit the following MSDN Web site: For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

307208 Why text appears different when drawn with GDIPlus versus GDI


Modification Type:MajorLast Reviewed:8/10/2004
Keywords:kbForms kbFont kbGDIPlus kbtshoot kbprb KB873197 kbAudDeveloper