Alignment problem when TextAlign = Middle in labels

K

Kyle Blaney

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?
 
C

Christoph Nahr

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.

I haven't used MiddleRight alignment myself but I had similar problems
with Label.AutoSize -- the bottom line in a multi-line label would be
cut off because the label was slightly too small.

What fixed the problem for me was using Graphics.MeasureString and
then manually setting the label size. (Ironically, the MSDN Library
claims that Label.AutoSize uses MeasureString internally...)

Maybe you could use the same method -- use differently sized labels to
exactly fit the text, as determined by MeasureString, then manually
right-align all the labels?

Here's the code fragment for getting the correct text size, assuming
the current object is the control that hosts your labels:

string text = /* text to display */;

Size textSize = Size.Empty;
using (Graphics paint = CreateGraphics())
textSize = Size.Ceiling(paint.MeasureString(text, Font));

Label textLabel = new Label();
textLabel.Text = text;
textLabel.UseMnemonic = false;
textLabel.Location = /* whatever */;
textLabel.Size = textSize;
Controls.Add(textLabel);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top