UserControl.Size=Parent.ClientSize;

  • Thread starter Thread starter Martijn Mulder
  • Start date Start date
M

Martijn Mulder

I want my custom UserControl to size and resize with its parent, a Form. But
when I use the code

Size=Parent.ClientSize;

in the constructor of myUserControl, Parent is null and the operation fails.
What is the proper place to put this code?
 
Well, Size is a struct (value-type), so this wouldn't really do what you
want anyway...

Perhaps you should look more at docking and anchoring?

Marc
 
Hi,


Most probably it is done in the InitializeComponent method when you add the
control to the Controls collection

I also suggest you to use docking/anchoring.
 
Thank you all,

docking/anchoring, it must be a lot like Java's LayoutManagers. What classes
and/or namespaces do I need to look into?
 
They are properties on the control instance; e.g. to fill the available
space (which sounds like what you want):

control.Dock = DockStyle.Fill; // could be this.Dock in a ctor

If, however, you want to just maintain the distance from the edges (w/o
filling everything), then

control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom| AnchorStyles.Left |
AnchorStyles.Right;

(or remove the edges you don't want to stick to; Top | Left is common, as is
Top | Left | Right)

Marc
 
Back
Top