2. When the app executes, another compiler (known as the just-in-time compiler
14.4 Control Properties and Layout
about the control’s class. In the left column of the page are several links to more informa- tion about the class—Button Constructor,Button Methods,Button PropertiesandButton
Events. This list may vary by class. Each link displays a subset of the class’s members. Click the link to the list of events for that control (Button Eventsin this case) to display the sup- ported events for that control.
Next, click the name of an event to view its description and examples of its use. We selected theClickevent to display the information in Fig. 14.10. TheClickevent is a member of classControl, an indirect base class of classButton. TheRemarkssection of the page discusses the details of the selected event. Alternatively, you could use theObject
Browser to look up this information in the System.Windows.Forms namespace. The
Object Browsershows only the membersoriginally defined in a given class. TheClick
event is originally defined in classControland inherited intoButton. For this reason, you must look at classControlin theObject Browserto see the documentation for theClick event. See Section 10.12 for more information regarding theObject Browser.
14.4 Control Properties and Layout
This section overviews properties that are common to many controls. Controls derive from classControl(namespaceSystem.Windows.Forms). Figure 14.11 lists some of class
Control’s properties and methods. The properties shown here can be set for many con- trols. For example, theTextproperty specifies the text that appears on a control. The lo- cation of this text varies depending on the control. In aForm, the text appears in the title bar, but the text of aButtonappears on its face.
Fig. 14.10 | Clickevent details.
Event argument class Event name Event type
TheSelectmethod transfers the focus to a control and makes it theactive control.
When you press theTabkey in an executing WindowsForms app, controlsreceive the focus in the order specified by theirTabIndexproperty. This property is set by Visual Studio based on the order in which controls are added to aForm, but you can change thetabbing order usingVIEW > Tab Order.TabIndexis helpful for users who enter information in many con- trols, such as a set ofTextBoxes that represent a user’s name, address and telephone number.
The user can enter information, then quickly select the next control by pressing theTabkey.
TheEnabledproperty indicates whether the user can interact with a control to gen- erate an event. Often, if a control isdisabled, 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 copies some text. In most cases, a disabled control’s text appears in gray (rather than in black). You can also hide a control from the user without disabling the control by setting theVisibleproperty tofalseor by calling methodHide. In each case, the control still exists but is not visible on theForm.
Anchoring and Docking
You can use anchoring and docking to specify the layout of controls inside a container (such as aForm).Anchoringcauses controls to remain at a fixed distance from the sides of
ClassControl
properties and
methods Description
Common Properties
BackColor The control’s background color.
BackgroundImage The control’s background image.
Enabled Specifies whether the control isenabled(i.e., if the user can interact with it). Typically, portions of adisabledcontrol appear “grayed out” as a visual indication to the user that the control is disabled.
Focused Indicates whether the controlhas the focus(only available at runtime).
Font TheFontused to display the control’s text.
ForeColor The control’s foreground color. This usually determines the color of the text in theTextproperty.
TabIndex Thetab orderof the control. When theTabkey is pressed, the focus transfers between controls based on the tab order. You can set this order.
TabStop Iftrue, then a user can give focus to this control via theTabkey.
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 theVisibleproperty tofalse).
Select Acquires the focus.
Show Shows the control (sets theVisibleproperty totrue).
Fig. 14.11 | ClassControlproperties and methods.
14.4 Control Properties and Layout 531
the container even when the container is resized. Anchoring enhances the user experience.
For example, if the user expects a control to appear in a particular corner of the app, an- choring ensures that the control will always be in that corner—even if the userresizesthe
Form.Dockingattaches a control to a container such that the control stretches across an entire side or fills an entire area. For example, a button docked to the top of a container stretches across the entire top of that container, regardless of the width of the container.
When a window (or other type ofparent containerlike aPanel) 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 theForm. To see the effects of anchoring a control, create a simple Windows
Forms app that contains twoButtons. Anchor one control to the right and bottom sides by setting theAnchorproperty as shown in Fig. 14.12. Leave the other control with its default anchoring (top, left). Execute the app and enlarge theForm. Notice that theButton anchored to the bottom-right corner is always the same distance from theForm’s bottom- right corner (Fig. 14.13), but that the other control stays its original distance from the top- left corner of theForm.
Sometimes, it’s desirable for a control to span an entire side of theForm, even when theFormis resized. For example, a control such as a status bar typically should remain at the bottom of theForm. Docking allows a control tospan an entire side(left, right, top or bottom) of its parent container or tofill the entire container. When the parent control is Fig. 14.12 | Manipulating theAnchorproperty of a control.
Fig. 14.13 | Anchoring demonstration.
Click the down-arrow in the Anchorproperty to display the anchoring window
Darkened bars indicate the container’s side(s) to which the control is anchored;
use mouse clicks to select or deselect a bar Anchoring
window
Before resizing After resizing
Constant distance to right and bottom sides
resized, the docked control resizes as well. In Fig. 14.14, aButtonis docked at the top of theForm(spanning the top portion). When theFormis resized, theButtonis resized to the
Form’s new width.Forms have aPaddingproperty that specifies the distance between the docked controls and theFormedges. This property specifies four values (one for each side), and each value is set to0by default. Some common control layout properties are summa- rized in Fig. 14.15.
TheAnchorandDockproperties of aControlare set with respect to theControl’s parent container, which could be aFormor another parent container (such as aPanel; dis- cussed in Section 14.6). The minimum and maximumForm(or otherControl) sizes can be set via propertiesMinimumSizeandMaximumSize, respectively. Both are of typeSize, which has propertiesWidthandHeightto specify the size of theForm. PropertiesMini-
mumSizeandMaximumSizeallow you to design the GUI layout for a given size range. The user cannot make aForm smaller than the size specified by propertyMinimumSize and cannot make aFormlarger than the size specified by propertyMaximumSize. To set aForm
to afixedsize (where theFormcannot be resized by the user), set its minimum and max- imum size to the same value.
Fig. 14.14 | Docking aButtonto the top of aForm.
Controllayout
properties Description
Anchor Causes a control to remain at a fixed distance from the side(s) of the con- tainer 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 is0, 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 aSizeobject, which has prop- ertiesWidthandHeight.
MinimumSize,
MaximumSize
Indicates the minimum and maximum size of aControl, respectively.
Fig. 14.15 | Controllayout properties.
Before resizing After resizing
Control extends along entire top portion of form