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 Win 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 matches 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.
To anchor a control on a form at run time
[Visual Basic] Private Sub AnchorMyControl() 'Set the TextBox control' to anchor to left and right. txtMain.Anchor = AnchorStyle.LeftRight End Sub [C#] private void AnchorMyControl() { ' Set the TextBox control' to anchor to left and right. txtMain.Anchor = AnchorStyle.LeftRight; }
When your form is displayed at run time, the control resizes to remain positioned at the same distance from the left and right edges of the form. The distance from the anchored edge always remains the same as the distance defined when the control is created.
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.
Control Manipulation on Win Forms | Controls by Category | Adding Controls to Win Forms Docking Controls on Win Forms | Layering Objects on Win Forms Setting the Tab Order on Win Forms | Working with Individual Controls | Controls You can Use on Win Forms | Control Technologies and Where to Use Them | Win Form Controls by Function