Tag Archives: Csha

Dock property of control in c#

 
You can set the control borders to docked to its parent control through Dock property. Dock property of any control determines how a control is resized with its parent control.means how a control automatically resized as its parent control is resized. for example if you set the dock property of a control to Left, then it automatically align with the left side of its parent and it will automatically resize as the parent control is resized.

Dock property
Dock Locations

There is one point to need consider here that Anchor and Dock properties are mutually exclusive. Only one can be set at a time and the last one set takes precedence.

Example

The following c# example creates a Panel and sets some of its common properties and then set the Dock property of this to Top.
and then set the Dock property of this to Top.

private void CreatePanelAndSetDock()
{
	Panel Pnl = new Panel();
	Pnl.BackColor = Color.Gray;
	Pnl.Height = 50;
	Pnl.BorderStyle = BorderStyle.Fixed3D;
	this.Controls.Add(Pnl);
	Pnl.Dock = DockStyle.Top;
}