简体   繁体   English

标签从右向左增长

[英]Label grow from right to left

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? 我该如何实现这一目标?

I solved this by setting the label 我通过设置标签解决了这个问题

AutoSize property to false , AutoSize属性为false

TextAlign to MiddleRight , 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. 视觉效果是一样的。

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 ; 我做的是将标签放在TableLayoutPanel控件中,并将TableLayoutPanel的RightToLeft属性设置为True ; this did the trick. 这样做了。

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: 你不能让它“从右向左成长”,但是你可以指定它的Left属性,这样它就不会超出表格:

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. 如果设计允许,您还可以将其高度加倍,以使文本跨越两行。

You can use the TableLayoutPanel or other compatible container control, but instead setting RightToLeft property for the container set Dock="Right" for the label 您可以使用TableLayoutPanel或其他兼容的容器控件,而是为容器设置RightToLeft属性设置Dock =“Right”作为标签

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. 设置RightToLeft属性并不总是给出预期的结果,因为某些字符串格式的字符串被修改,改变了单词的顺序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM