Docking Form.UserControl

  • Thread starter Thread starter Hananiel
  • Start date Start date
H

Hananiel

I have a User control that I load dynamically into a Form. The designer has
no property to let me dock it. How do i Dock it or get it to stretch out to
fill. Setting this.Dock = DockStyle.Fill; in the constructor did not work.
Neither did implementing the resize event.

thanks,
Hananiel
 
Hi Hananiel
Setting controls Dock property has to do the work.
The steps you should follow are:
1. Instantiate an objects of your control class.
MyControl ctrl = new MyControl();

2. Set all properties you need as well as the Dock property
ctrl.Dock = DockStyle.Fill;

3. Add the control as a child control to other contol or form

hostCtrl.SuspendLayout();
hostCtrl.Controls.Add(ctrl);
hostCtrl.ResumeLayout();

Suspend and resume layout methods pair don't have to be called if you add
only one control but they are very helpful when you add more then one.

Bare in mind that the control classes layout their children in backward
order that mans the las control added will be layouted first so if the host
control have already childern and you add the new control with filling dock
style that control will be layouted first and then the others. So, your
filling cotnrol will be either obscured by the others or will cover the
others. This is not what you want to achieve, I guess. So you have to make
sure that the filling control is always at index 0.
If you have already children of the host control you has to take one step
more when you add the control. You have to move it at index 0. Control
collection doesn't support Insert method and you should do it like this:

hostCtrl.SuspendLayout();
hostCtrl.Controls.Add(ctrl);
hostCtrl.Controls.SetChildIndex(ctrl, 0);
hostCtrl.ResumeLayout();

HTH
B\rgds
100
 
Back
Top