Files
docs-desktop/dotnet-desktop-guide/net/winforms/controls/how-to-dock-and-anchor.md
T
Andy (Steve) De GeorgeandTom Dykstra f2a6c1b0e9 Port dock/anchor articles (#1081)
* Port some more info from autosize

* Port anchor/dock articles

* Apply suggestions from code review

Co-authored-by: Tom Dykstra <[email protected]>

* Move construction note

Co-authored-by: Tom Dykstra <[email protected]>
2021-06-01 16:32:00 -07:00

6.1 KiB

title, description, ms.date, dev_langs, helpviewer_keywords
title description ms.date dev_langs helpviewer_keywords
How to dock and anchor controls Learn how to s 05/25/2021
csharp
vb
Anchor property [Windows Forms], enabling resizable forms
Windows Forms controls, screen resolutions
resizing forms [Windows Forms]
Windows Forms controls, size
screen resolution and control display
controls [Windows Forms], anchoring
forms [Windows Forms], resizing
Windows Forms, resizing
controls [Windows Forms], positioning
controls [Windows Forms], docking
Explorer-style applications [Windows Forms], creating
Windows Forms controls, filling client area

How to dock and anchor controls (Windows Forms .NET)

If you're designing a form that the user can resize at run time, the controls on your form should resize and reposition properly. Controls have two properties that help with automatic placement and sizing, when the form changes size.

[!INCLUDE desktop guide under construction]

For more information, see Position and layout of controls.

Dock a control

A control is docked by setting its xref:System.Windows.Forms.Control.Dock%2A property.

Note

Inherited controls must be Protected to be able to be docked. To change the access level of a control, set its Modifier property in the Properties window.

Use the designer

Use the Visual Studio designer Properties window to set the docking mode of a control.

  1. Select the control in the designer.

  2. In the Properties window, select the arrow to the right of the Dock property.

    :::image type="content" source="media/how-to-dock-and-anchor/vs-dock-property.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Dock property shown.":::

  3. Select the button that represents the edge of the container where you want to dock the control. To fill the contents of the control's form or container control, press the center box. Press (none) to disable docking.

    :::image type="content" source="media/how-to-dock-and-anchor/vs-dock-property-expanded.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Dock property expanded.":::

The control is automatically resized to fit the boundaries of the docked edge.

Set Dock programmatically

  1. Set the Dock property on a control. In this example, a button is docked to the right side of its container:

    button1.Dock = DockStyle.Right;
    
    button1.Dock = DockStyle.Right
    

Anchor a control

A control is anchored to an edge by setting its xref:System.Windows.Forms.Control.Anchor%2A property to one or more values.

Note

Certain controls, such as the xref:System.Windows.Forms.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.

Use the designer

Use the Visual Studio designer Properties window to set the anchored edges of a control.

  1. Select the control in the designer.

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

    :::image type="content" source="media/how-to-dock-and-anchor/vs-anchor-property.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Anchor property shown.":::

  3. To set or unset an anchor, select the top, left, right, or bottom arm of the cross.

    :::image type="content" source="media/how-to-dock-and-anchor/vs-anchor-property-expanded.png" alt-text="Visual Studio Properties pane for .NET Windows Forms with Anchor property expanded.":::

Set Anchor programmatically

  1. Set the Anchor property on a control. In this example, a button is anchored to the right and bottom sides of its container:

    button1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    
    button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
    

See also