Label grow from right to left
Solution 1
My problem here was that my label was in a panel, and anything I did wasn't working.
What I did was to place the label in a TableLayoutPanel
control and set the TableLayoutPanel's RightToLeft
property to True
; this did the trick.
Solution 2
I solved this by setting the label
AutoSize property to false,
TextAlign to MiddleRight,
an Anchor to the right.
Notice that the label size itself is not growing with the text, but you can handle it by giving it enough width to fit the content. The visual effect is the same.
Solution 3
You can't make it "grow from right to left", but you can assign it's Left
property so that it won't go out of the form:
label1.Text = "some dynamic text here...";
if (label1.Right > this.Width)
label1.Left = this.Width - label1.Width;
If the design allows it, you can also double its height so that the text will span two lines.
Solution 4
You can use the TableLayoutPanel or other compatible container control, but instead setting RightToLeft property for the container set Dock="Right" for the label
Setting RightToLeft property does not always give the expected results as for some string formats the string is modified changing the order of the words.
Solution 5
you can Write it:
public enum Leftorright { left,right}
private Leftorright _LeftToRight = Leftorright.left;
public Leftorright LeftToRight
{
get { return _LeftToRight; }
set { _LeftToRight = value; }
}
protected override void OnTextChanged(EventArgs e)
{
int oldWidth;
oldWidth = this.Width;
base.OnTextChanged(e);
if (LeftToRight == Leftorright.right && this.Width != oldWidth)
{
this.Left = this.Left - this.Width + oldWidth;
}
}
Yash
Updated on July 05, 2022Comments
-
Yash almost 2 years
I have a label on my form which is on the right of the form. This label loads a dynamic text.
Sometimes the text that it loads is too long and the text crosses the border of the form, that is some of the text is out of the form.
I want to make the label to grow from right to left instead of left to right. How do I achieve this?
-
Gravitate over 6 yearsI used this to extend the existing label control. However, I had problems with the label moving it's width to the left when the text is first set (because it's initial width is 0). To fix this, I just added a check that oldWidth>0.
-
Martin Braun about 4 yearsIt's not the same visual effect and is not working properly when your label covers anything with a different background color, like a
PictureBox
. -
Alejandro del Río about 4 years@MartinBraun You can just use a transparent background. The visual effect is still the same.
-
Martin Braun about 4 yearsThank you for your reply, good attempt, but sorry, I was a little bit unspecific: My label has a background color, so it covers the
PictureBox
below it where the font of the label is, so it can be read properly. UsingAutoSize
false will require me to use a greater width, thus more of myPictureBox
will be covered than needed. Basically I wanted my solid background color of my label to only be below the label font itself, yet I wanted it to be able to grow to the left. Don't stress yourself out with it, I'm fine for left aligning in my case, it's just a limitation I wanted to point out. -
Alejandro del Río about 4 yearsPersonally, I rather use transparent colors for label backgrounds. It's much better from a design point of view but that's subjective. Regards!
-
Shahaboddin about 2 yearsI'm newbie in C# and this
TableLayoutPanel
you introduced here is answer to many responsive winform design problems! Twitter's Bootstrap basically is the same thing and web using it all over the place. Best solution -
Shahaboddin about 2 yearsnot a responsive dynamic solution. Or at least, reinventing the wheel (handle AutoSize by urself).
-
Jay Croghan almost 2 yearsThis is the answer I didn't know I even needed! Thank you.