The most obvious way to right-align a Label
in WinForms doesn\'t work: setting anchor to Top/Bottom Right and TextAlign to TopRight. If the text changes the lab
Using a FlowLayoutPanel to do it works very well.
flowLayoutPanel.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
flowLayoutPanel2.Controls.Add(label);
Then, just make sure that the flowLayoutPanel is large enough for the label to expand.
Well as Sphax noticed you have to:
AutoSize
to falseTextAlign
to Right, for example to MiddleRight
MeasureString
Code:
label.AutoSize = false;
label.TextAlign = ContentAlignment.MiddleRight;
int yourWidthHere = 100;
using (Graphics g = label.CreateGraphics())
{
SizeF size = g.MeasureString(text, label.Font, yourWidthHere);
label.Height = (int)Math.Ceiling(size.Height);
label.Text = text;
}
if you set the form property RightToLeft = yes; so you should not use the Text Align property just set the Anchor. try this approaches:
Form.righttoleft = yes;
label.anchor = Top, Right;
label.TextAlign = TopLeft;
or
Form.righttoleft = No;
label.anchor = Top, Right;
label.TextAlign = TopRight;
or
Form.righttoleft = yes;
label.righttoleft = No;
label.anchor = Top, Right;
label.TextAlign = TopRight;