vb.net 标签位置应该固定在右边并向左增长

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

Label position should fixed right and grow to left

vb.netwinforms

提问by SwissGuy

How can I set my labels to align on the right even when they have diffrent lenghts. I have a set of labels which are occuring next to each other and also underneath each other.
The problem now is that they always align from the left within the label row,but I need them to align on the right as they are showing sums from other rows.

Just to verify I am not talking about the text align I am looking for a solution to align my labels.

即使标签具有不同的长度,我如何将标签设置为在右侧对齐。我有一组标签,它们彼此相邻,也彼此下方。
现在的问题是它们总是从标签行内的左侧对齐,但我需要它们在右侧对齐,因为它们显示来自其他行的总和。

只是为了验证我不是在谈论文本对齐,我正在寻找对齐标签的解决方案。

Thanks in advance

提前致谢

回答by Hans Passant

Simply set the AutoSize property to False in the designer. Adjust the size to fit the column. Then set the TextAlign to one of the right-alignment ones.

只需在设计器中将 AutoSize 属性设置为 False。调整大小以适合列。然后将 TextAlign 设置为右对齐之一。

回答by Matt Wilko

You should be able to do it at runtime using the following code:

您应该能够在运行时使用以下代码执行此操作:

'find the current right alignment position
Dim rightAlign As Integer = Label1.Left + Label1.Width
'set the text (assumes AutoSize is set to True)
Label1.Text = value
'adjust position so the right hand point is in the same position as before
Label1.Left = rightAlign - Label1.Width

回答by Tom Stephan

My method is even more strange. I create the labels and then when laying out the fields for the report adjust the labels for number (etc) that are to be right aligned Note: all labels end with 'lbl' - txtNew is the report column text box. - get the column's left edge plus the width of the column minus the width of the label. Works! Just not my favorite way to do it.

我的方法更奇怪。我创建标签,然后在布置报告的字段时调整要右对齐的数字(等)标签注意:所有标签都以“lbl”结尾 - txtNew 是报告列文本框。- 获取列的左边缘加上列的宽度减去标签的宽度。作品!只是不是我最喜欢的方式。

    ' *** NEED TO CALC POSITION FOR RIGHT JUSTIFY OF LABEL !!!!!
    If ShouldRightJustify(rs.Fields(i).Type) Then
        rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Left = _
            (lblCol + txtNew.Width) _
            - rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Width
        End If

回答by Bradley Uffner

If you are asking how to do this from the designer, use the Format Menu. Select all the controls you want to align, then click the control you want the other aligned to. Do Format > Align > Rights.

如果您向设计人员询问如何执行此操作,请使用格式菜单。选择要对齐的所有控件,然后单击要其他控件对齐的控件。做格式>对齐>权限。

If you are trying to do this at run-time you can loop through the controls you want to align and set their .X property according to their width. For example. To align a label so that it's right side is at X=200... SomeLabel.X = 200 - SomeLabel.Width.

如果您尝试在运行时执行此操作,您可以遍历要对齐的控件并根据它们的宽度设置它们的 .X 属性。例如。对齐标签,使其右侧位于 X=200... SomeLabel.X = 200 - SomeLabel.Width。