Label grow from right to left

C#.NetWinformsLabel

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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 2 - C#

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 3 - C#

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

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

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;
        }
    }

Solution 6 - C#

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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYashView Question on Stackoverflow
Solution 1 - C#Alejandro del RíoView Answer on Stackoverflow
Solution 2 - C#YashView Answer on Stackoverflow
Solution 3 - C#Shadow Wizard Says No More WarView Answer on Stackoverflow
Solution 4 - C#armadillo.mxView Answer on Stackoverflow
Solution 5 - C#mohammad faridvandView Answer on Stackoverflow
Solution 6 - C#user10678882View Answer on Stackoverflow