Erroneous Size.Height (or width) for newly initialized windows control

From here
The ‘x’ in the following code
public class myPanel: System.Windows.Forms.Panel
{
protected Control inner;
public myPanel(Control _ctrl)
{
inner = _ctrl;
this.Controls.Add(inner);
int x = inner.Size.Height;
}
}

Will always show a value of ‘100’ despite whatever the actual value of Size.Height is. But if you run control through the ‘x=’ assignment statement after a quick watch on ‘inner’, you’ll get the right value.
This can be fixed by calling the Control.CreateControl() method which causes the Size.Height to be initialized to the correct value.
public class myPanel: System.Windows.Forms.Panel
{
protected Control inner;
public myPanel(Control _ctrl)
{
inner = _ctrl;
inner.CreateControl();
this.Controls.Add(inner);
int x = inner.Size.Height;
x=55;
}
}

Comments

Archive

Show more