14.4 Control Properties and Layout

This section overviews properties that are common to many controls. Controls derive from class Control (namespace System.Windows.Forms). Figure 14.11 lists some of class

Control’s properties and methods. The properties shown here can be set for many controls. For example, the Text property specifies the text that appears on a control. The location of this text varies depending on the control. In a Form, the text appears in the title bar, but the text of a Button appears on its face.

Fig. 14.11 Class Control properties and methods.

Class Control properties and methods Description
Common Properties
BackColor The control’s background color.
BackgroundImage The control’s background image.
Enabled Specifies whether the control is enabled (i.e., if the user can interact with it). Typically, portions of a disabled control appear “grayed out” as a visual indication to the user that the control is disabled.
Focused Indicates whether the control has the focus (only available at runtime).
Font The Font used to display the control’s text.
ForeColor The control’s foreground color. This usually determines the color of the text in the Text property.
TabIndex The tab order of the control. When the Tab key is pressed, the focus transfers between controls based on the tab order. You can set this order.
TabStop If true, then a user can give focus to this control via the Tab key.
Text The text associated with the control. The location and appearance of the text vary depending on the type of control.
Visible Indicates whether the control is visible.
Common Methods
Hide Hides the control (sets the Visible property to false).
Select Acquires the focus.
Show Shows the control (sets the Visible property to true).

Method Select transfers the focus to a control and makes it the active control. When you press the Tab key in an executing Windows Forms app, controls receive the focus in the order specified by their TabIndex property. This property is set by Visual Studio based on the order in which controls are added to a Form, but you can change the tabbing order using View > Tab Order. TabIndex is helpful for users who enter information in many controls, such as a set of TextBoxes that represent a user’s name, address and telephone number. The user can enter information, then quickly select the next control by pressing the Tab key.

Property Enabled indicates whether the user can interact with a control to generate an event. Often, if a control is disabled, it’s because an option is unavailable to the user at that time. For example, text editor apps often disable the “paste” command until the user cuts or copies some text. In most cases, a disabled control’s text appears in gray (rather than in black). You also can hide a control from the user without disabling the control by setting the Visible property to false or by calling method Hide. In each case, the control still exists but is not visible on the Form.

14.4.1 Anchoring and Docking

You can use anchoring and docking to specify the layout of controls inside a container—such as a Form or, within a Form, a control that groups other controls (such as a Panel, discussed in Section 14.6). Anchoring places controls a fixed distance from the sides of the container. Anchoring enhances the user experience. For example, if the user expects a control to appear in a particular corner of the app, anchoring ensures that the control will always be in that corner—even if the user resizes the Form. Docking attaches a control to a container such that the control stretches across an entire side or fills an entire area. For example, a control such as a status bar typically should remain at the bottom of the Form and stretch across the entire bottom of that Form, regardless of the Form’s width. When the parent control is resized, the docked control resizes as well.

Anchoring Demonstration

When a window (or other type of container like a Panel) is resized, anchored controls are moved (and possibly resized) so that the distance from the sides to which they’re anchored does not vary. By default, most controls are anchored to the top-left corner of the Form. To see the effects of anchoring a control, create a simple Windows Forms app that contains two Buttons. Anchor one control to the right and bottom sides by setting the Anchor property as shown in Fig. 14.12. Leave the other control with its default anchoring (top, left). Execute the app and enlarge the Form. The Button anchored to the bottom-right corner is always the same distance from the bottom right (Fig. 14.13). The other control stays its original distance from the top left.

Fig. 14.12 Manipulating the Anchor property of a control.

Fig. 14.13 Anchoring demonstration.

In Fig. 14.14, a Button is docked at the top of the Form (spanning the top portion). When the Form is resized, the Button is resized to the Form’s new width. Forms have a Padding property that specifies the distance between the docked controls and the Form edges. This property specifies four values (one for each side), and each value is set to 0 by default. Some common control layout properties are summarized in Fig. 14.15.

Fig. 14.14 Docking a Button to the top of a Form.

Fig. 14.15 Control layout properties.

Control layout properties Description
Anchor Causes a control to remain at a fixed distance from the side(s) of the container even when the container is resized.
Dock Allows a control to span one side of its container or to fill the remaining space in the container.
Padding Sets the space between a container’s edges and docked controls. The default is 0, causing the control to appear flush with the container’s sides.
Location Specifies the location (as a set of coordinates) of the upper-left corner of the control, in relation to its container’s upper-left corner.
Size Specifies the size of the control in pixels as a Size object, which has properties Width and Height.
MinimumSize, MaximumSize Indicates the minimum and maximum size of a Control, respectively.

A Control’s Anchor and Dock properties are set with respect to the container in which the Control resides—known as the parent container—which could be a Form or another container (such as a Panel). The minimum and maximum Form (or other Control) sizes can be set via properties MinimumSize and MaximumSize, respectively. Both are of type Size, which has properties Width and Height. Properties MinimumSize and MaximumSize allow you to design the GUI layout for a given size range. The user cannot make a Form smaller than the size specified by property MinimumSize and cannot make a Form larger than the size specified by property MaximumSize. To set a Form to a fixed size (where the Form cannot be resized by the user), set its minimum and maximum size to the same value.

14.4.2 Using Visual Studio To Edit a GUI’s Layout

Visual Studio helps you with GUI layout. When you drag a control across a Form, blue snap lines appear to help you position the control with respect to others (Fig. 14.16) and the Form’s edges. This feature makes the control you’re dragging appear to “snap into place” alongside other controls. Visual Studio also provides the Format menu, which contains options for modifying your GUI’s layout. The Format menu does not appear in the IDE unless you select one or more controls in design view. When you select multiple controls, you can align them with the Format menu’s Align submenu. The Format menu also enables you to modify the space between controls or to center a control on the Form.

Fig. 14.16 Snap lines for aligning controls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset