[英]Label grow from right to left
我的表格上有一個標簽,位於表格的右側。 此標簽加載動態文本。
有時它加載的文本太長,文本越過表單的邊框,這是一些文本不在表單中。
我想讓標簽從右到左而不是從左到右。 我該如何實現這一目標?
我通過設置標簽解決了這個問題
AutoSize屬性為false ,
TextAlign to MiddleRight ,
一個向右的錨點 。
請注意,標簽大小本身並沒有隨文本增長,但您可以通過為其提供足夠寬度來適應內容來處理它。 視覺效果是一樣的。
我的問題是我的標簽是在一個小組中,我做的任何事情都沒有用。
我做的是將標簽放在TableLayoutPanel
控件中,並將TableLayoutPanel的RightToLeft
屬性設置為True
; 這樣做了。
你不能讓它“從右向左成長”,但是你可以指定它的Left
屬性,這樣它就不會超出表格:
label1.Text = "some dynamic text here...";
if (label1.Right > this.Width)
label1.Left = this.Width - label1.Width;
如果設計允許,您還可以將其高度加倍,以使文本跨越兩行。
您可以使用TableLayoutPanel或其他兼容的容器控件,而是為容器設置RightToLeft屬性設置Dock =“Right”作為標簽
設置RightToLeft屬性並不總是給出預期的結果,因為某些字符串格式的字符串被修改,改變了單詞的順序。
你可以寫下來:
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.