76

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Yash
  • 2,259
  • 4
  • 26
  • 33
  • My problem here was that my label was in a panel, and anything I wouldnt get this working. What I did was to place the label in a tablelayoutpanel control and set rtl to yes. this did the trick. – Yash Apr 10 '11 at 18:15
  • Ok guys. I did not think of posting it as an anwser :p thx for the tips. – Yash Apr 10 '11 at 20:16

7 Answers7

94

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.

Alejandro del Río
  • 3,966
  • 3
  • 33
  • 31
  • It's not the same visual effect and is not working properly when your label covers anything with a different background color, like a `PictureBox`. – Martin Braun Mar 28 '20 at 06:21
  • @MartinBraun You can just use a transparent background. The visual effect is still the same. – Alejandro del Río Mar 29 '20 at 20:46
  • Thank 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. Using `AutoSize` false will require me to use a greater width, thus more of my `PictureBox` 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. – Martin Braun Mar 30 '20 at 10:22
  • Personally, I rather use transparent colors for label backgrounds. It's much better from a design point of view but that's subjective. Regards! – Alejandro del Río Apr 07 '20 at 22:00
  • not a responsive dynamic solution. Or at least, reinventing the wheel (handle AutoSize by urself). – Shahaboddin Mar 26 '22 at 01:07
32

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.

zastrowm
  • 8,017
  • 3
  • 43
  • 63
Yash
  • 2,259
  • 4
  • 26
  • 33
  • I'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 Mar 26 '22 at 01:05
  • This is the answer I didn't know I even needed! Thank you. – Jay Croghan Jun 21 '22 at 15:47
6

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.

armadillo.mx
  • 934
  • 1
  • 11
  • 17
6

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
3

Wrap the label inside a FlowLayoutPanel and set the following properties in the panel:

  • Anchor to right;
  • AutoSize to GrowAndShrink;
  • FlowDirection to RightToLeft
1

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;
        }
    }
  • I 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. – Gravitate Nov 23 '17 at 11:50
1
using System.Windows.Forms;

/// <summary>
/// The position of myLabel to the left of the otherControl component when entering 
/// text "s". 
/// You must set myLabel.AutoSize = true
/// </summary>
/// <param name="s">text</param>
void WriteText(string s)
{
    int len = TextRenderer.MeasureText ( s, myLabel.Font ).Width;
    myLabel.Left = otherControl.Left - 5 - len;
    myLabel.Text = s;
}