When labels are vertically stacked on top of one another and have
their TextAlign property set to MiddleRight, the text is not properly
right-aligned.
You can reproduce this problem by creating a blank form with three
private members (label1, label2, and label3) and adding the following
code to the form's constructor:
// BEGIN CODE
label1 = new Label();
label2 = new Label();
label3 = new Label();
// Use a fixed size, X position and content alignment for every label.
//
Size size = new Size(256, 16);
int xPos = 104;
Content content = ContentAlignment.MiddleRight;
label1.Location = new Point(expos, 24);
label1.Size = size;
label1.Text = ":Lisa Kudrow:";
label1.TextAlign = contentAlignment;
label2.Location = new Point(xPos, 40);
label2.Size = size;
label2.Text = ":Jennifer Aniston:";
label2.TextAlign = contentAlignment;
label3.Location = new Point(xPos, 56);
label3.Size = size;
label3.Text = ":Courtney Cox:";
label3.TextAlign = contentAlignment;
Controls.Add(label1);
Controls.Add(label2);
Controls.Add(label3);
// END CODE
When the form is opened, the rightmost colons in the labels are not
vertically aligned. They are close, but the colon in ":Lisa Kudrow:"
is slightly further right than the colons is "Jennifer Aniston" and
"Courtney Cox".
Note that when the contentAlignment is set to
ContentAlignment.MiddleLeft, the leftmost colons are perfectly
vertically aligned. Therefore, the problem appears to be with the
"MiddleRight" text alignment only.
The only way I can get text to be vertically aligned on the right is
to set each label's RightToLeft property to RightToLeft.Yes and
TextAlign property to MiddleLeft. This solution has obvious
drawbacks.
Is there a better solution?