Making Controls Arrange
Themselves
Lesson 2 explained how to add controls to a form and arrange them nicely. Using those tech-
niques, you can create forms like the one shown in Figure 3-1.
FIGURE 31
That form looks okay in Figure 3-1, but what if the user enlarges the form as shown in
Figure 3-2? Pretty lame, huh? Although the form is bigger, the areas that contain data are not.
FIGURE 32
3
596906c03.indd 37 4/7/10 12:31:38 PM
38
LESSON 3 Making Controls arrange theMselves
The URL for the book selected in Figure 3-2 is too long to fit within the GroupBox, so it is truncated
even though the form has extra wasted space on the right. The
ListBox isn’t big enough to display all
of its items even though there’s wasted space at the bottom. It would be nice if the controls rearranged
themselves to use the available space and display the entire URL and more list items.
Figure 3-3 shows another problem with this form.
If the user shrinks the form, the
TextBoxes and
URL
LinkLabel are chopped off, the Year Label
and
TextBox are chopped in half vertically, the
ListBox doesnt fit, and the cover picture is com-
pletely missing.
The program would look nicer if the controls were
shrunk so you could at least see their edges. Some
of the values still wouldn’t fit but at least the form
wouldnt look so amateurish. You could even make the form refuse to shrink so it’s too short to dis-
play the Year controls.
This lesson explains some simple ways you can make controls rearrange themselves to take advantage
of whatever space is available, and how to give the form minimum and maximum sizes so the user
can’t make it completely useless.
RESTRICTING FORM SIZE
Forms (and in fact all controls) have MinimumSize and MaximumSize properties that you can use
to restrict the form’s size. Simply set these properties to a width and height (or set their
Width and
Height sub-properties) and the form does the rest.
For example, to prevent the user from making the form shown in Figure 3-3 too small, you can set
the forms
MinimumSize property to 663, 233.
USING ANCHOR PROPERTIES
The MinimumSize property prevents the user from making a form too small but it doesn’t solve the
problem shown in Figure 3-2. When the user resizes a form, it would be nice for controls to change
their sizes to match.
The
Anchor property lets a control resize itself when its container resizes. This property can take one
or more of the values
Top, Bottom, Left, and Right, in any combination. These values indicate that
the control’s edge should remain the same distance from the corresponding edge of its container.
For example, initially a control’s
Anchor property is set to Top Left so it remains the same distance
from its container’s top and left edges. If you resize the form, the control doesn’t move.
For a more interesting example, suppose you place a
TextBox on a form, set its Multiline property
to
True, arrange it so its edges are 12 pixels from the edges of the form, and set its Anchor property to
Top, Bottom, Left, Right. Then when you resize the form, the TextBox resizes itself so its edges
remain 12 pixels from the form’s edges.
FIGURE 33
596906c03.indd 38 4/7/10 12:31:39 PM

Get Stephens' C# Programming with Visual Studio® 2010 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.