Udostępnij za pośrednictwem


How to: Anchor 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.

The Anchor property interacts with the AutoSize property. For more information, see AutoSize Property Overview.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Working with Settings.

To anchor a control on a form

  1. 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.

  2. In the Properties window, click the arrow to the right of the Anchor property.

    An editor is displayed that shows a cross.

  3. 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.

  4. To clear a side of the control that has been anchored, click that arm of the cross.

  5. To close the Anchor property editor, click the Anchor property name again.

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 Modifiers property in the Properties window.

See Also

Tasks

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

Concepts

AutoSize Property Overview

Other Resources

Windows Forms Controls

Arranging Controls on Windows Forms