windows 将 Winforms 中的标签调整到左侧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3187086/
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-15 14:46:42  来源:igfitidea点击:

Resizing labels in Winforms to Left

c#.netwindows.net-2.0

提问by Nagesh

I have 15 to 20 labels with variable text sizes and text boxes arranged in the forms. Text boxes are arranged next to labels. The font and color of the form and hence the form controls can be configured by the user at run time. When I right align the labels and set auto grow property to true and whenever the font style changes (say from Arial to Georgia) the right aligned labels are no more right aligned.

我有 15 到 20 个标签,它们具有可变的文本大小和排列在表单中的文本框。文本框排列在标签旁边。用户可以在运行时配置表单的字体和颜色以及表单控件。当我右对齐标签并将自动增长属性设置为 true 时,每当字体样式发生变化(比如从 Arial 到 Georgia)时,右对齐的标签不再右对齐。

I need a solution on labels (for winforms) to automatically grow to the left when the font size changes.

我需要一个关于标签的解决方案(对于 winforms),以便在字体大小改变时自动向左增长。

回答by bmkorkut

Set your label properties as folowing;

将您的标签属性设置如下;

AutoSize = false;
TextAlign = TopRight (or anything to right)

Then manually resize your label to a maximum area to fit your longest text. That worked for me.

然后手动将标签大小调整到最大区域以适合最长的文本。那对我有用。

回答by Lazarus

Set the anchor to "Right" rather than "Left" (you will probably also have "Top" in which case it's "Right Top" rather than "Left Top"), it should grow in the right (left) direction I believe. Been a while since I did any of this so try it and let me know how it goes.

将锚点设置为“右”而不是“左”(您可能还会有“顶部”,在这种情况下它是“右上”而不是“左上”),我相信它应该朝右(左)方向生长。我已经有一段时间没有这样做了,所以试试吧,让我知道它是怎么回事。

回答by Daniel Brückner

You could probably use a TableLayoutPanel.

您可能可以使用TableLayoutPanel.

回答by serhio

You could use also RightToLeft label propertyinstead of modifying Anchor.

您也可以使用RightToLeft 标签属性而不是修改 Anchor。

回答by nathant23

I had the same problem. My fix was to create a simple function to move the label, which I called after any event or code that changed the label size.

我有同样的问题。我的解决方法是创建一个简单的函数来移动标签,我在任何更改标签大小的事件或代码之后调用该函数。

Enter the name of the label you want to grow left, and the X coordinate of the right end of the label. Then you call this function after any changes to the label.

输入要向左生长的标签的名称,以及标签右端的 X 坐标。然后在对标签进行任何更改后调用此函数。

private void repositionLabel(Label lab, int endPoint)
        {
            lab.Left = endPoint - lab.Width; 
        }

For example, you have a label named myLabel positioned at (75,75) and it currently has a width of 25 and you always want it to end at (100,75). Then when this happens:

例如,您有一个名为 myLabel 的标签位于 (75,75),它当前的宽度为 25,您总是希望它以 (100,75) 结束。那么当发生这种情况时:

myLabel.Text = "blah blah blah blah blah blah";

you then follow the text change with:

然后,您可以按照以下文本更改:

repostitionLabel(myLable, 100);

This will make it look like the label expanded to the left.

这将使标签看起来像向左扩展。