简体   繁体   中英

Calculate and set location of label at design time

On a form I got a TabControl, and above this a Label. I always want the label to appear centered above the TabControl. The width of the TabControl changes (with anchors) if the width of the form is changed. 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?

Winforms aren't that dynamic at design time, like @o_O alreday mentioned in his comment.

But in your case there's no need to center the Label programmically.
Like following, the label-Text will also be centered at design time:

  • Set the AutoSize -Property of the Label to False

  • Adjust the width of the Label to the same width as the TabControl

  • Set the Anchor -Property of the Label to Top, Left, Right

  • Set the TextAlign -Property of the Label to TopCenter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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