Anchoring Controls on Windows Forms
If you are designing a form that the user can resize at run time, the controls on your form should resize and reposition properly. To resize controls dynamically with the form, you can use the Anchor property of Windows Forms controls. The Anchor property defines 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. For example, if you have a Textbox control that is anchored to the left, right, and bottom edges of the form, as the form is resized, the Textbox control resizes horizontally so that it maintains the same distance from the right and left sides of the form. In addition, the control positions itself vertically so that its location is always the same distance from the bottom edge of the form. If a control is not anchored and the form is resized, the position of the control relative to the edges of the form is changed.
To anchor a control on a form
- Select the control you want to anchor.
Note You can anchor multiple controls simultaneously by pressing the CTRL key, clicking each control to select it, and then following the rest of this procedure.
- In the Properties window, click the arrow to the right of the Anchor property.
An editor is displayed that shows a cross.
- To set an anchor, click the top, left, right, or bottom section of the cross. Controls are anchored to the top and left by default. To clear a side of the control that has been anchored, click that arm of the cross.
Click the Anchor property name again to close the Anchor property editor.
When your form is displayed at run time, the control resizes to remain positioned at the same distance from the edge of the form. The distance from the anchored edge always remains the same as the distance defined when the control is positioned in the Windows Forms Designer.
Note Certain controls, such as the ComboBox control, have a limit to their height. Anchoring the control to the bottom of its form or container cannot force the control to exceed its height limit.
Inherited controls must be Protected to be able to be anchored. To change the access level of a control, set its Modifier property in the Properties window.
Windows Forms Controls | Controls by Category | Adding Controls to Windows Forms | Arranging Controls on Windows Forms | Labeling Individual Windows Forms Controls and Providing Shortcuts to Them | Controls You Can Use on Windows Forms | Windows Forms Controls by Function | Creating Windows Forms