How to: Anchor controls on Windows Forms
If you're 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.
Anchor a control on a form
In Visual Studio, select the control you want to anchor.
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.
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.
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
Modifiers property in the Properties window.
- Windows Forms Controls
- AutoSize Property Overview
- How to: Dock Controls on Windows Forms
- Walkthrough: Arranging Controls on Windows Forms Using a FlowLayoutPanel
- Walkthrough: Arranging Controls on Windows Forms Using a TableLayoutPanel
- Walkthrough: Laying Out Windows Forms Controls with Padding, Margins, and the AutoSize Property