Arranging Controls


The beauty of the Designer is that it lets you lay out your controls lovingly within your form, making sure everything lines up nicely , as shown in Figure 1.5.

Figure 1.5. Nicely Laid-Out Form at Ideal Size

But then someone resizes it, as shown in Figure 1.6.

Figure 1.6. Nicely Laid-Out Form Resized

The user isn't resizing the form to get more gray space but to make the controls bigger so that they will hold more data. For that to happen, the controls need to resize to take up the newly available space. You can do this manually by handling the form's Resize event and writing the code. Or you can do it with anchoring.

Anchoring is one of the ways that WinForms provides for automatic layout control of your forms and the controls contained therein. By default, all controls are anchored to the upper-left, so that as the form is resized and moved, all controls are kept at their position relative to the upper-left corner of the form. However, in this case, we'd clearly like to have the text box controls widen or narrow as the form is resized. We implement this by setting each text box's Anchor property. In the Property Browser for the control, we choose the Anchor property, which displays an editor like the one in Figure 1.7.

Figure 1.7. Setting the Anchor Property

To change the text boxes so that they anchor to the right edge as well as the top and left edges is a matter of clicking on the anchor rectangle on the right and changing the Anchor property to Top, Left, Right. This will cause the text boxes to resize as the form resizes, as shown in Figure 1.8.

Figure 1.8. Anchoring Text Boxes Top, Left, Right and Anchoring Buttons Bottom, Right

The default anchoring is top-left, but those edges need not be a part of the anchoring settings at all. For example, notice that Figure 1.8 anchors the OK and Cancel buttons to the bottom-right, as is customary with Windows dialogs.

If instead of building a dialog-style form, you'd like to build a window-style form, anchoring is not your best bet. For example, suppose you're building an Explorer-style application, with a menu bar and toolbar on the top, a status bar on the bottom, and a tree view and a list view taking up the rest of the space as determined by a splitter between them. In that kind of application, anchoring won't do. Instead, you'll want docking.

Docking allows you to "stick" any control on the edge of its container, the way a status bar is stuck to the bottom of a form. By default, most controls have the Dock property set to None (the default for the StatusBar control is Bottom). You can change the Dock property in the Property Browser by picking a single edge to dock to or to take up whatever space is left, as shown in Figure 1.9.

Figure 1.9. Setting the Dock Property

As an example, the form in Figure 1.10 shows the Dock properties for a status bar, a tree view, and a list view, the latter two being split with a splitter control. You can arrange all this without writing a line of code.

Figure 1.10. Docking and Splitting

Anchoring, docking, and splitting are not the only ways to arrange controls on a form. WinForms also lets you group controls and handle custom layout for special situations. In addition, WinForms supports arranging windows within a parent, which we call MDI. These techniques are all covered in detail in Chapter 2: Forms.



Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net