在任何形式的 VB.NET 上居中标签

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

Center a label on any form VB.NET

vb.netwidthmeasuretext-rendering

提问by soulblazer

I need to show a form with a centered label (according to form's width and label's text, width, font family and font size). This have been my attempt so far:

我需要显示一个带有居中标签的表单(根据表单的宽度和标签的文本、宽度、字体系列和字体大小)。到目前为止,这是我的尝试:

(Me.Width - TextRenderer.MeasureText("Hello word", New Font("Delius", 10, 
FontStyle.Regular).Width) / 2

No matter how much I try, the label doesn't appear centered as it should (label's left and right sides don't appear to be the same size).

无论我尝试多少次,标签都不会像它应该的那样居中(标签的左右两侧看起来不一样)。

Is there another way to measure text no matter which font is used? Thank you.

无论使用哪种字体,还有另一种方法来测量文本吗?谢谢你。

回答by Jens

Set the Autosizeproperty of your label to False, then either Dockthe Label Top, Bottom or Fill, or drag it to the full width of the form and set Anchorto both Left and Right. Then set TextAlignto MiddleCenter.

Autosize标签的属性设置为 False,然后Dock设置 Label Top、Bottom 或 Fill,或者将其拖动到表单的整个宽度并设置Anchor为 Left 和 Right。然后设置TextAlign为 MiddleCenter。

The Anchorproperty is pretty nifty, because it basically pins the a border of a control to the respective side of the form.
So in our case the left side of the control sticks to the left side of the form, and the right side sticks to the right side of the form.
So if the form is resized, it drags the left and right side of the control with it. Together with the TextAlign, this always keeps the text centered.
For this to work, the AutoSizefunctionality of the label needs to be disabled.

Anchor属性非常漂亮,因为它基本上将控件的边框固定到表单的相应侧。
所以在我们的例子中,控件的左侧粘在窗体的左侧,而右侧则粘在窗体的右侧。
因此,如果调整窗体的大小,它会随之拖动控件的左侧和右侧。与 一起TextAlign,这始终使文本居中。
为此,AutoSize需要禁用标签的功能。

An alternative way would be to keep AutoSizeenabled, center the form on the control, and then disableboth Left and Right Anchor. This would keep the label centered as well, as it now does no longer stick to either side but keeps its relative position.

另一种方法是保持AutoSize启用状态,将表单居中放在控件上,然后同时禁用Left 和 Right Anchor。这也将保持标签居中,因为它现在不再粘在任一侧,而是保持其相对位置。

So: Let the control do the work for you.

所以:让控件为您完成工作。

enter image description here

在此处输入图片说明

回答by VBNETcoder

Here is a more professional solution:

这是一个更专业的解决方案:

horizontal centering:

水平居中:

 myLabel.Left = (myLabel.Parent.Width ) - (myLabel.Width )

vertical centering:

垂直居中:

myLabel.Top = (myLabel.Parent.Height \ 2) - (myLabel.Height \ 2)

add this code on the myLabel.[SizeChanged][1]Event handler as well as on its parent SizeChangedEvent handler

将此代码添加到myLabel.[SizeChanged][1]事件处理程序及其父SizeChanged事件处理程序上

p.s. dont add the codeline before the InitializeComponent()method is called or before the control being attached to a parent control.

ps 不要在InitializeComponent()调用方法之前或在控件附加到父控件之前添加代码行。