.net WinForms 中的右对齐标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1692383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:32:41  来源:igfitidea点击:

Right-aligned labels in WinForms

.netwinformslabel

提问by Roman Starkov

The most obvious way to right-align a Labelin WinForms doesn't work: setting anchor to Top/Bottom Right and TextAlign to TopRight. If the text changes the label's Left coordinate remains unchanged instead of the Right coordinate (which, one might argue, is a bug).

Label在 WinForms 中右对齐 a 的最明显方法不起作用:将锚点设置为顶部/底部右侧并将 TextAlign 设置为顶部右侧。如果文本更改,则标签的左坐标保持不变,而不是右坐标(有人可能会争辩说,这是一个错误)。

For this reason I've always used a full-width TableLayoutPanelfor right-aligned labels. However this is not always very convenient, depending on the layout in question...

出于这个原因,我一直TableLayoutPanel对右对齐标签使用全角。然而,这并不总是很方便,这取决于所讨论的布局......

So, I wonder if there are any other ways to keep a Label right-aligned in WinForms that never occurred to me?

所以,我想知道是否还有其他方法可以让我从未想过的 WinForms 中的 Label 保持右对齐?

回答by Marc Gravell

One simple option is to disable AutoSize(set to false) and over-size it so there is spare space.

一个简单的选择是禁用AutoSize(设置为false)并放大它以便有空闲空间。

Alternatively, perhaps use Dockinstead of just Anchor, although this has a different meaning, so you may need to put it in a Panelor similar). Ultimately this works like the first - by over-sizing it in the first place; so perhaps the first option is simpler.

或者,也许使用Dock而不仅仅是Anchor,尽管这具有不同的含义,因此您可能需要将其放在 aPanel或类似中)。最终这就像第一个一样 - 首先是过度调整它;所以也许第一个选择更简单。

回答by Damien

Using a TableLayoutPanel with docked labels is the only reliable method that I've found for placing right-aligned labels in Winforms. Turning off AutoSize and using oversized labels seems to cause strange anomalies for High DPI users.

使用带有停靠标签的 TableLayoutPanel 是我发现的在 Winforms 中放置右对齐标签的唯一可靠方法。对于高 DPI 用户来说,关闭 AutoSize 并使用超大标签似乎会导致奇怪的异常。

回答by Aaron Deming

Using a FlowLayoutPanel to do it works very well.

使用 FlowLayoutPanel 来做这件事效果很好。

flowLayoutPanel.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
flowLayoutPanel2.Controls.Add(label);

Then, just make sure that the flowLayoutPanel is large enough for the label to expand.

然后,只需确保 flowLayoutPanel 足够大以便标签展开。

回答by Kelvyn

Here's what worked for me on a standard form

这是在标准表格上对我有用的内容

  • Set AutoSize property off for just the Labels to be right aligned
  • Make all the fields the same size (perhaps this isn't really required) using the Layout toolbar
  • Multi-select the labels and right align them using the Layout toolbar, position where desired
  • Set TextAlign property to one of the xxxRight settings, e.g, TopRight
  • 将 AutoSize 属性设置为 off 仅使标签右对齐
  • 使用布局工具栏使所有字段的大小相同(也许这不是真正必需的)
  • 多选标签并使用布局工具栏将它们右对齐,放置在所需位置
  • 将 TextAlign 属性设置为 xxxRight 设置之一,例如 TopRight

回答by Andrey Danilov

Well as Sphaxnoticed you have to:

正如斯帕克斯注意到的那样,你必须:

  1. Set AutoSizeto false
  2. Set TextAlignto Right, for example to MiddleRight
  3. Resize label to real size using MeasureString
  1. 设置AutoSize为假
  2. 设置TextAlign为右,例如MiddleRight
  3. 使用 MeasureString

Code:

代码:

label.AutoSize = false; 
label.TextAlign = ContentAlignment.MiddleRight;    

int yourWidthHere = 100;    
using (Graphics g = label.CreateGraphics())    
{    
     SizeF size = g.MeasureString(text, label.Font, yourWidthHere);    
     label.Height = (int)Math.Ceiling(size.Height);    
     label.Text = text;    
}   

回答by bh_earth0

  • dynamically created label's default autosize is false.
  • if label's autosize is false. it contains extra empty space.
  • that tricks you to think its doesnt right align properly. to diagnose it, set the label's backColour to lightgreen
  • 动态创建的标签的默认自动调整大小为 false。
  • 如果标签的自动调整大小为假。它包含额外的空白空间。
  • 这会让你认为它没有正确对齐。要诊断它,请将标签的 backColour 设置为浅绿色

enter image description here

在此处输入图片说明

 int rowIndex=1;

 var lbx = new Label();
 lbx.AutoSize = true;          // default is false.
 lbx.BackColor = Color.Green;  // to see if it's aligning or not
 lbx.Text = "Iam  Autosize=true";
 lbx.Anchor = AnchorStyles.Right;
 tlPanel.Controls.Add(lbx, 0, rowIndex);

 var dtp = new DateTimePicker();
 dtp.Anchor = AnchorStyles.Left;
 tlPanel.Controls.Add(dtp, 1, rowIndex);


  //--- row 2  autosize false
 rowIndex=2;
  var lbx2 = new Label();
 lbx2.AutoSize = false;          // default is false.
 lbx2.BackColor = Color.Green;  // to see if it's aligning or not
 lbx2.Text = "AutoSz=false";
 lbx2.Anchor = AnchorStyles.Right;
 tlPanel.Controls.Add(lbx2, 0, rowIndex);

 var dtp = new DateTimePicker();
 dtp.Anchor = AnchorStyles.Left;
 tlPanel.Controls.Add(dtp, 1, rowIndex);

回答by Wael Dalloul

if you set the form property RightToLeft = yes; so you should not use the Text Align property just set the Anchor. try this approaches:

如果您设置表单属性 RightToLeft = yes; 所以你不应该只使用 Text Align 属性来设置锚点。试试这个方法:

Form.righttoleft = yes;
label.anchor = Top, Right;
label.TextAlign = TopLeft;

or

或者

Form.righttoleft = No;
label.anchor = Top, Right;
label.TextAlign = TopRight;

or

或者

Form.righttoleft = yes;
label.righttoleft = No;
label.anchor = Top, Right;
label.TextAlign = TopRight;

回答by Sphax

The best solution for me was:

对我来说最好的解决方案是:

  1. Set the AutoSize property labels to false. Set the TextAlign
  2. property labels to something on the right.
  3. Resize manually the labels 1 by 1 so they can use more space.
  1. 将 AutoSize 属性标签设置为 false。设置文本对齐
  2. 属性标签到右边的东西。
  3. 手动调整标签 1 x 1 的大小,以便它们可以使用更多空间。

回答by Rob

Attach an event handler to the labels' SizeChanged event:

将事件处理程序附加到标签的 SizeChanged 事件:

private void label1_SizeChanged(object sender, EventArgs e)
{
    label1.Location = new Point(Your_Anchor_Point - label1.Width, label1.Location.Y);
}

To be more DPI friendly consider using some other control as the anchor point, i.e.

为了更加 DPI 友好,请考虑使用其他控件作为锚点,即

label1.Location = new Point(dataGridView1.Location.X + dataGridView1.Width - label1.Width, label1.Location.Y);

to align to the RH side of the dgv.

对齐到 dgv 的 RH 侧。

(BTW: I tried the Paint & TextChanged events but they seemed to sometimes get confused - probably something to do with event order particularly on opening a new form.)

(顺便说一句:我尝试了 Paint & TextChanged 事件,但它们似乎有时会感到困惑 - 可能与事件顺序有关,特别是在打开新表单时。)