如何在 WinForms 中保持标签居中?
在 WinForms
中,我使用 Label
来显示不同的消息,例如成功、失败等。
我想将该标签置于中心表单的中心。我想要一种解决方案,无论标签中只有一个单词还是整个句子,它都能保持居中。
In WinForms
I am using a Label
to display different messages like success, failure, etc.
I'd like to center that label in the center form. I want a solution that will keep it centered whether there's just one word or a whole sentence in the label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将
Label
的AutoSize
属性设置为False
,将TextAlign
属性设置为MiddleCenter
和 <将 code>Dock 属性设置为Fill
。Set
Label
'sAutoSize
property toFalse
,TextAlign
property toMiddleCenter
andDock
property toFill
.您将通过设置属性 Anchor: None 来实现它。
You will achive it with setting property Anchor: None.
用于以编程方式设置的一些次要附加内容:
Dockstyle 和内容对齐可能与您的需求不同。例如,对于 wpf 表单上的简单标签,我使用 DockStyle.None。
Some minor additional content for setting programmatically:
Dockstyle and Content alignment may differ from your needs. For example, for a simple label on a wpf form I use DockStyle.None.
如果您不想将标签停靠在整个可用区域中,只需设置 SizeChanged 事件而不是 TextChanged 即可。当 autosize 属性设置为 True 时,更改每个字母将更改标签的宽度属性及其文本。因此,顺便说一句,您可以使用任何公式来保持标签在表单中居中。
If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.
接受的答案对我不起作用有两个原因:
BackColor
,因此设置AutoSize = false
和Dock = Fill
会导致背景无论如何,AutoSize
设置为 false,因为我的标签文本是动态的,相反,我只是使用表单的宽度和标签的宽度来计算左侧偏移量:
The accepted answer didn't work for me for two reasons:
BackColor
set so settingAutoSize = false
andDock = Fill
causes the background color to fill the whole formAutoSize
set to false anyway because my label text was dynamicInstead, I simply used the form's width and the width of the label to calculate the left offset:
我想做类似的事情,但在带有背景图像的表单上,我发现当标签中的文本更改时,使用此方法重新绘制很明显,所以我执行了以下操作:
* 将标签 AutoSize 设置为 true,将 TextAlign 设置为 MiddleCenter
然后,每次文本更改(我的文本更改是使用计时器完成的)时,我都会调用以下方法:
并将标签的 Location 属性设置为此返回值。这确保了当文本更改时标签始终位于表单的中心,并且全屏表单的重新绘制并不明显。
I wanted to do something similar, but on a form with a background image, I found that when the text in the label changed the repaints were obvious with this method, so I did the following:
* Set the label AutoSize to true and TextAlign to MiddleCenter
Then, each time the text changed (mine was done using a timer) I called the following method:
And set the label's Location property to this return value. This ensured that the label was always in the center of the form when the text changed and the repaints for a full-screen form weren't obvious.
您可以尝试以下代码片段:
It's Indeed Center
You could try out the following code snippet:
It's Really Center
'这。'是您所在的表格,其中 lblName 作为您想要居中的项目。此外,“offsetInt”将允许您将标签定位在中心的左侧或右侧。 lblName.Location.Y 维护表单上现有的 Y 高度。
'this.' being the form you are on with lblName as the item you wish to centre. Additionally 'offsetInt' will allow you to position the label left or right of the centre. lblName.Location.Y maintains the existing Y height on the form.