简体   繁体   中英

How do I align my text in a label to the right side?

So here is my file label. If the label is too long, it will go off the screen thus making me have to scroll.

I've tried the following properties (separate times):

  • RightToLeft as Yes
  • ContentAlignment as BottomRight or TopRight

However, none of them are working. What is the correct property?

文件标签

Try setting AutoSize to false (that makes a fixed size box). Then you can use the TextAlign property to align the text - eg use MiddleRight . That should do the trick.

Label label = new Label();
label.AutoSize = false;    
label.TextAlign = ContentAlignment.MiddleRight;

In the properties window:

  • Set AutoSize = False
  • Set TextAlign = MiddleRight

In the designer:

  • Resize the label so it's wide enough to fit whatever potential text you try to set it to.

只需删除 autosize = true并设置RightToLeft = true ,标签现在将增长到左侧。

Yes AutoSize = false and RightToLeft = true will work. Note that when you set RightToLeft, the strange c# 2010 TextAlign TopLeft becomes aligned correctly to the right. If I set that to Top/Middle Right, the text jumps to the left instead!

Sample (WPF):

Label label = new Label();
label.HorizontalContentAlignment = HorizontalAlignment.Right;

This worked for me:

your_label_name.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

所有你需要做的标签才能使它工作:

  1. Textalign = MiddleRight
  2. Anchor = top and right

设置Autosize=trueTextalign=MiddleRight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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