Dynamic Layout in Windows FormsThe controls on a form can be dynamically resized as the form is resized. Windows Forms provides three ways to control the layout of your form:
AnchoringWhen a control is anchored to the edge of its container, the distance between the control and the specified edge remains constant when the container is resized. A control can be anchored to any combination of the edges of the container. If the control is anchored to opposite edges of its container, it is resized when the container is resized. For example, if you anchor a TextBox control to the left and right edges of a form, then as the form is resized the width of the TextBox changes. The following code anchors a TextBox to the upper, left, and right edges of its container: textBox1.Anchor = AnchorStyles.Top BitOr AnchorStyles.Left BitOr AnchorStyles.Right VB
To view and run this sample:
DockingWhen a control is docked to the edge of its container, it remains in contact with that edge when the container is resized. For example, in Windows Explorer, the TreeView control is docked on the left edge of the window and the ListView control is docked on the right edge of the window. If multiple controls are docked to one edge, the first is docked to the edge of the container, and the others are docked as close as possible to the edge without covering the others. The following code docks a Panel control on the left edge a the container. Panel1.Dock = DockStyle.Left VB
To view and run this sample:
DockMan SampleThe following sample allows you to manipulate the anchoring and docking properties of a Button at run time so that you can understand how they interact.
Custom LayoutYou can write your own layout manager by using the Layout event that is exposed on the Control class. This event is raised whenever any event occurs that causes the control to display child controls. These events include Resize, show/hide child controls, and add/remove child controls. This event should be used to perform any custom layout logic.
|