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?
我想让标签从右到左而不是从左到右。我该如何实现这一目标?
5 个解决方案
#1
59
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.
请注意,标签大小本身并没有随文本增长,但您可以通过给予足够的宽度来适应内容来处理它。视觉效果是一样的。
#2
22
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.
我做的是将标签放在TableLayoutPanel控件中,并将TableLayoutPanel的RightToLeft属性设置为True;这样做了。
#3
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:
你不能让它“从右向左成长”,但是你可以指定它的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.
如果设计允许,您还可以将其高度加倍,以使文本跨越两行。
#4
3
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属性并不总是给出预期的结果,因为某些字符串格式的字符串被修改,改变了单词的顺序。
#5
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;
}
}
#1
59
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.
请注意,标签大小本身并没有随文本增长,但您可以通过给予足够的宽度来适应内容来处理它。视觉效果是一样的。
#2
22
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.
我做的是将标签放在TableLayoutPanel控件中,并将TableLayoutPanel的RightToLeft属性设置为True;这样做了。
#3
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:
你不能让它“从右向左成长”,但是你可以指定它的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.
如果设计允许,您还可以将其高度加倍,以使文本跨越两行。
#4
3
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属性并不总是给出预期的结果,因为某些字符串格式的字符串被修改,改变了单词的顺序。
#5
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;
}
}