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;
}
}
22
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;
}
6 回答
我的问题是我的标签是在一个面板中,我做的任何事情都没有用 .
我做的是将标签放在
TableLayoutPanel
控件中,并将TableLayoutPanel的RightToLeft
属性设置为True
;这样做了 .您可以't make it 2403815 , but you can assign it' s
Left
属性,以便它不会超出表单:如果设计允许,您还可以将其高度加倍,以使文本跨越两行 .
您可以使用TableLayoutPanel或其他兼容的容器控件,而是为容器设置RightToLeft属性设置Dock =“Right”作为标签
设置RightToLeft属性并不总是给出预期的结果,因为某些字符串格式的字符串被修改,改变了单词的顺序 .
我通过设置标签解决了这个问题
AutoSize property to false ,
TextAlign 至 MiddleRight ,
Anchor 到 right .
请注意,标签大小本身并没有随文本增长,但您可以通过为其提供足够宽度来适应内容来处理它 . 视觉效果是一样的 .
你可以写下来: