What is Anchoring a control and Docking a control?
Anchoring Property- The forms can be created in such a way that they can be resizeable and for this we need to adjust one of the values of the FormBorderStyle property that is to configure the reusability of the form.
Need for Anchoring - The Anchor property of Windows Forms controls is used to resize the controls dynamically with the form.
- It describes an anchor position for the control. When a control is anchored to a form and the form is resized, the control maintains the distance between the control and the anchor positions.
Docking Property- The dock property controls the edges of your form or have them fill the controls container (either a form or a container control).
- An example can be, the Windows Explorer docks its TreeView control to the left side of the window and its ListView control to the right side of the window.
- Whenever a control is docked to an edge of it's container it will always be positioned flush against that edge while the container re-sizes.
- If there are more than one control and is docked to an edge, the controls will not be placed on top of each other.
- If we select Left or Right then the left and right edge of the control is re-sized the same size as the containing control's edge.
- Likewise, if the Top or Bottom is selected then the top and bottom edge of the control is re-sized to the same size as the containing control's edge.
- If Fill is selected, all sides of the control is re-sized to the same size as all the containing control's edges.
-
For example, to make a Button control fill the client area of its container, the code is as follows:
button1.Dock = DockStyle.Fill;
- This control is automatically resized to fit the boundaries of the docked edge.