简体   繁体   English

在设计时计算并设置标签位置

[英]Calculate and set location of label at design time

On a form I got a TabControl, and above this a Label. 在表单上,​​我得到了一个TabControl,在其上方有一个Label。 I always want the label to appear centered above the TabControl. 我一直希望标签显示在TabControl上方的中心位置。 The width of the TabControl changes (with anchors) if the width of the form is changed. 如果更改窗体的宽度,则TabControl的宽度将更改(使用锚点)。 Now I want the position (location) of the label to be adjusted accordingly. 现在,我要相应地调整标签的位置(位置)。

At runtime this is easy: 在运行时,这很容易:

//Position headline label
lblHeadline.Left = tabControl1.Left + (tabControl1.Width / 2) - (lblHeadline.Width / 2);

But how do I get Visual Studio to do this at design time, so if the width of the form is changed from the properties window, the label's position is automatically recalculated? 但是如何在设计时使Visual Studio做到这一点,所以如果从属性窗口更改了表单的宽度,则会自动重新计算标签的位置?

Winforms aren't that dynamic at design time, like @o_O alreday mentioned in his comment. Winform在设计时并不是那么动态,就像@o_O alreday在他的评论中提到的那样。

But in your case there's no need to center the Label programmically. 但是在您的情况下,无需以编程方式将Label居中。
Like following, the label-Text will also be centered at design time: 如下所示,标签文本也将在设计时居中:

  • Set the AutoSize -Property of the Label to False 将标签的AutoSize -Property设置为False

  • Adjust the width of the Label to the same width as the TabControl 将Label的宽度调整为与TabControl相同的宽度

  • Set the Anchor -Property of the Label to Top, Left, Right 将标签的Anchor属性设置为Top, Left, Right

  • Set the TextAlign -Property of the Label to TopCenter 将标签的TextAlign -Property设置为TopCenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM