Reputation: 14075
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
Upvotes: 1
Views: 220
Reputation: 6849
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;
Upvotes: 2