Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

The Anchor property preserves the distance from the control to the anchored edge or edges of its container. Here, the container for the button and picture box controls is the Form itself. There are also other containers such as the Panel and GroupBox controls that we will encounter in chapters 7 and 9 that can hold anchored controls as well.

You can think of an anchor as being much like a boat tethered to a floating pier at the edge of a lake. The lake is “resized” as the water level rises and falls, but the distance of the boat from the pier remains constant based on the length of the tether.

For example, if a control is anchored 10 pixels from the left edge of its container, it will remain 10 pixels from the left edge regardless of the size of the container. If a control is anchored to opposite sides then the control expands or shrinks so that the distance from its edges to the anchored edges remain constant.

In our code, the Button is anchored to the top and left of the form, which is the default setting for the Anchor property. As a result our Load button remains in the upper left corner of the display window as the form is resized. The PictureBox control is anchored to all four sides so that it expands as the application window expands and shrinks as the window shrinks.

TRY IT! Change the Anchor settings in your program to experiment with this property. In particular, set this property for the btnLoad control to AnchorStyles.None. You will find that the control moves half the distance the form is resized in this case. Expand the form by 10 pixels horizontally, and the Load button will be 5 additional pixels from the left edge.

While you’re at it, take out the MinimumSize property and see what happens. For the more adventurous, use the desktop properties from figure 1.7 such as ControlBox and MaximumSize to see the effect on your program.

1.4.3The Dock property

The use of Anchor is fine when you have a set of controls and need to define their

resize behavior. In the case where you want to use as much of the form as possible, the Anchor property does not quite work. While you could position the control at the edges of the form and anchor it to all sides, this is not the most elegant solution. Instead the framework provides the Dock property for this purpose.

The Dock property is related to Anchor in that it also affects the resizing of controls on a form. In our previous analogy of the boat tethered to a floating pier, the boat itself is “docked” to the shore, in that it remains at the edge of the lake as the water rises and falls. Similarly, the Dock property establishes a fixed location for a control within its container by fixing it flush against a side of the form.

Like Anchor, the Dock property takes its values from an enumeration, in this case the DockStyle enumeration. Note that one enumeration is plural (AnchorStyles) since a control can be anchored to multiple sides, while the other enumeration is singular (DockStyle) since a control is docked to no sides, one side, or all sides. More details on this enumeration appear in .NET Table 1.4.

RESIZING FORMS

31

.NET Table 1.4 DockStyle enumeration

The DockStyle enumeration specifies the settings available for the Dock property in the Control class, and by inheritance all controls in the .NET Framework. This enumeration is part of the System.Windows.Forms namespace. If a Dock property other than None is set for a control then the Anchor setting for that control is set to the top and left edges

 

Bottom

Control is positioned flush against the bottom edge of its

 

 

container.

 

Fill

Control is positioned flush against all sides of its container.

 

Left

Control is positioned flush against the left edge of its container.

Enumeration

None

Control is not docked to its container. This is the default, and

values

 

indicates that the Anchor property is used to maintain the

 

 

 

 

control’s position within its container.

 

Right

Control is positioned flush against the right edge of its

 

 

container.

 

Top

Control is positioned flush against the top edge of its container.

 

 

 

We can see how the Dock property works by changing our program so that the PictureBox control fills the entire form. Also change the version number in your program code to 1.5 (not shown here).

pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2;

pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;

pboxPhoto.Dock = DockStyle.Fill;

// Add our new controls to the Form

this.Controls.Add(btnLoad);

this.Controls.Add(pboxPhoto);

Compile and run the program again. After loading an image your form should look something like figure 1.8. Note how the Load button is still visible since it is added to the form first and is therefore higher in the z- order stack than the image.

Note that if multiple controls are set to the same Dock value, the z-order of the controls determines the order in which the controls are docked. The top, or first, control in the z-order stack is placed flush against the docked edge. The next control is placed flush against the first control, and so on. The exception is the DockStyle.Fill value. In this case the controls appear on top of one another, and the z-order determines which control is seen.

Figure 1.8 This PictureBox control in this window is docked to fill the entire client area of the form.

32

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

Соседние файлы в папке c#