简体   繁体   中英

Width of C# Label before it is shown?

I need to know which width the label will have with AutoSize = true; before the form is shown so I can position other controls relatively to the label. Actually I have no access to the form only to the parent Control.

(Working completely without Designer. That means just code.)
(Measure string in Graphics is unreliable so I can't use that.)

Label label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);            
label.Text = "hello world";
myParent.Controls.Add(label);

// more control generation follows, THEN form is shown

Ok, you cannot get the label width before adding it into its parent control. just place the location after Controls.Add

Label label = new Label();
label.AutoSize = true;
label.Text = "hello world";
myParent.Controls.Add(label);
label.Left = cntControl1.Left - label.Width;
label.Top = cntControl1.Top;

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