Shape.Anchor Property

 

Gets or sets the edges of the container to which a shape is bound, and determines how a shape is resized when its parent is resized.

Namespace:   Microsoft.VisualBasic.PowerPacks
Assembly:  Microsoft.VisualBasic.PowerPacks.Vs (in Microsoft.VisualBasic.PowerPacks.Vs.dll)

public virtual AnchorStyles Anchor { get; set; }

Property Value

Type: System.Windows.Forms.AnchorStyles

A bitwise combination of the AnchorStyles values. The default is Top and Left.

Use the Anchor property to define how a shape is automatically resized when its parent control is resized. Anchoring a shape to its parent control guarantees that the anchored edges remain in the same position relative to the edges of the parent control when the parent control is resized.

You can anchor a shape to one or more edges of its container. For example, assume that you have a Form that contains a RectangleShape whose Anchor property value is set to Top and Bottom. The RectangleShape is stretched to maintain the distance between the top and bottom edges of the Form as the Height of the Form is increased.

Notes to Inheritors:

When you override the Anchor property in a derived class, use the base class's Anchor property to extend the base implementation. Otherwise, you must provide all the implementation. You are not required to override both the get and set accessors of the Anchor property; you can override only one if you need to.

The following example shows how to use the Anchor property to resize shapes when a form is resized. This example requires that you have two or more shape or line controls on a form.

private void ResizeShapes()
{
    // Loop through the ShapeCollection.
    foreach (Shape shape in shapeContainer1.Shapes)
    {
        // Set the Anchor property.
        shape.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | 
            AnchorStyles.Right | AnchorStyles.Top;
    }
}
Return to top
Show: