The AutoSize property enables a control to change its size, if necessary, to fit the size specified by the PreferredSize property. The anchor is set with the Anchor property. As the container changes in size, any child control will maintain its distance to the anchored side.Ī control can be anchored to one or more sides, without restriction. If multiple controls are docked to the same side of the container, they're stacked according to their Z-order.Īnchoring a control allows you to tie the control to one or more sides of the parent container. The control that is docked to the top has less horizontal space available to it.Īs the container grows and shrinks, the controls docked to the container are repositioned and resized to maintain their applicable positions and sizes. The control uses the entire height of the container. If the control's Z-order is reversed, the control that is docked to the left now has more initial space available. If a next control is docked to the left, it has less vertical space available to it. For example, if a control is drawn first and docked to the top, it will take up the entire width of the container. As docked controls are laid out, they use what space is available to them. The Z-order of the control does affect docking. The Location of the control can't be manually set as docking a control automatically controls its position. If a control is docked to the left, the Width of the control is respected but the Height is automatically adjusted. For example, if the control is docked to the top, the Height of the control is respected but the Width is automatically adjusted. The width and height of the control are still respected based on the docking style. When a control is docked, the container determines the space it should occupy and resizes and places the control. The Dock property sets which border of the control is aligned to the corresponding side of the parent, and how the control is resized within the parent. The MinimumSize and MaximumSize properties help set the minimum and maximum space a control can use. Meaning, the collection is a first-in-last-drawn and last-in-first-drawn collection. Each control is drawn in the reverse order they appear in the collection. The order in which a control is drawn determined by the control's index in the parent's Controls collection. There are two properties on a control that help automatic placement and size within a parent: Dock and Anchor.ĭrawing order can affect automatic placement. Some parent containers force placement while others respect control settings that guide placement. For example, Visual Studio displays the snapline when you drag a control next to another control:Ĭontrols can be automatically placed within their parent. Snaplines appear as guides to help you remain outside the specified margin of a control. The Visual Studio Designer will respect these properties when you're positioning and resizing controls. The following figure shows the Margin and Padding properties on a control. The Padding property defines the space in the interior of a control that keeps the control's content (for example, the value of its Text property) a specified distance from the control's borders. The Margin property defines the space around the control that keeps other controls a specified distance from the control's borders. There are two control properties that help with precise placement of controls: Margin and Padding. The MaximumSize and MinimumSize properties help set the minimum and maximum space a control can use.
In this case, the position and size of the control may not be manually adjusted, depending on the type of parent. When a control is added to a parent that enforces automatic placement, the position and size of the control is changed. The size of the control is determined by the Size property and represents the width and height of the control. The top-left position coordinate in the parent is (x0,y0). The position a control appears on a parent is determined by the value of the Location property relative to the top-left of the parent surface.