Form and Control Layout
Windows Forms allows developers to lay out sophisticated user interfaces that are capable of intelligently resizing without writing a line of code. Previously, developers writing desktop applications for Windows typically spent a good deal of time writing resizing code to handle the placement of controls on the form when the user resized the form. The .NET platform, however, allows you to define a control’s layout and size by setting a few properties.
The Anchor Property
The Anchor property lets a control anchor to any or all sides of its container. Each anchored side of the control is kept within a constant distance from the corresponding side of its container. When the container is resized, its anchored controls are repositioned and resized as necessary to enforce this rule. The Anchor property is defined by the Control class (in the System.Windows.Forms namespace) and so is inherited by all controls and forms. Its syntax is:
Public Overridable Property Anchor( ) As System.Windows.Forms.AnchorStyles
The AnchorStyles type is an enumeration that defines the values
Left
, Top
,
Right
, Bottom
, and
None
. To anchor a control on more than one edge,
combine the values with the Or
operator, as shown
here:
' Assumes Imports System.Windows.Forms SomeControl.Anchor = AnchorStyles.Top Or AnchorStyles.Right
By default, controls are anchored on the top and left sides. This means that if a form is resized, its controls maintain a constant distance from the top and left ...
Get Programming Visual Basic .NET now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.