Properties of Controls

The Windows Form class inherits from the Control class. Of course, the Form class adds some extra behavior to the Control classfor instance, a form is a control that can contain other controls. Because the Form class inherits from the Control class, you'll find that many of the properties of controls are similar to those of a Windows form. As noted in Chapter 1, "Introducing Windows Forms," you may modify the properties of a form using the Properties window within the Visual Studio .NET interface (by clicking the form in order to select it and then modifying the values within the Properties window). Similarly, you can modify the properties of a control by first clicking the control and then using the Properties window.

You can also modify the properties of a control using code by assigning the desired value to the control's property using the format ControlObject.Property , where ControlObject is the name of the target control and Property is the name of the property to which the new value should be assigned.

graphics/tip_icon.gif

In addition to the properties you can set directly, controls have a z-order property that specifies the order in which controls are evaluated when layered atop one another on the z-axis. The control with the lowest z-order value is considered to be on top of others with higher z-order values. You can rapidly move a control's z-order location within its container by right-clicking the control and selecting Bring to Front or Send to Back from the shortcut menu. You can also call the BringToFront or SendToBack method of the control to alter its z-order.


The following sections review properties of controls that you may be expected to be familiar with for the exam.

Anchor

When a form is resized, you may want the controls to remain anchored to a particular location on the form so that the result remains in a standard layout. The default anchor for controls is the top-left corner of the form. This way, as the form is resized larger, the controls will remain in position at the top-left corner of the expanded form. You may adjust the Anchor value to use any sides of the form that you like within the Properties window, as shown in Figure 2.1.

Figure 2.1. The Properties window for a control showing the Anchor value selected with an assigned anchor at the bottom-left corner of the form.

graphics/02fig01.jpg

Dock

Dock specifies a value similar to Anchor , although here the control is positioned with regard to a side rather than a corner of the form. Docking is often used with toolbars and status bars in order to maintain their position even as the form is resized. You may adjust this value within the Properties window, as shown in Figure 2.2. Its default value is None, meaning that a control is not docked to an edge by default.

Figure 2.2. The Properties window for a control showing the Dock property selected for bottom-edge docking.

graphics/02fig02.jpg

Enabled

A control will have a Boolean (True/False) value assigned to its Enabled property. This value is generally True by default, although controls such as Timer have an Enabled value of False by default. If a control is not enabled, it will appear grayed out and will not raise any events or receive focus.

Font

Font describes the format for presenting textual information within the control. Figure 2.3 displays the Font dialog box used to set this property. Programmatic assignment of Font will fail with an error unless you create a new Font object and assign the new object to this property of the control. The Font dialog box is accessed in the Properties listing by clicking the ellipsis ( ) button.

Figure 2.3. The Font dialog box shows the Microsoft Sans Serif font selected in Italic mode at 12-point size.

graphics/02fig03.jpg

Location

This property allows for the specification of the absolute location of the top-left corner of the control measured in pixels from the top-left corner of the form. This value can be set within Visual Studio by placing the control graphically on the form or by setting the Location property of the control in the Properties window. The Location property is a struct value (that is, it consists of other propertiesin this case separate x and y coordinates). In the Properties window, you'll find a plus sign (+) next to the Location property's name. Click the plus sign to see the individual members of the struct.

Name

The name of a control is used to manipulate the control programmatically. The default value of the Name property is an automatically generated value based on the type of control and the order in which it is placed on the form, such as Label1, Label2, and Label3 for the first three Label controls added to a form.

graphics/tip_icon.gif

It is good coding practice to rename a control using a meaningful name and the three-character Hungarian naming scheme prefix to display what type of control it is. An example of this would be lblSalesDesc, which indicates a label box holding a description for the Sales form.


Size, Height, and Width

The Size property is a struct value consisting of the Height and Width properties so that you can easily manipulate one or both, as desired. The size for a new control is assigned based on the control's DefaultSize value. It can be altered as desired in order to fit on the application's form.

TabIndex and TabStop

The Tab key can be used to allow a user to rapidly change focus from one control to the next. The TabStop property is a Boolean (True/False) value, set to True by default, that indicates whether a control receives the focus when a user is tabbing between controls. If this value is set to False, the control is skipped over. If TabStop is set to True, the control receives focus based on its order within the other controls, established by the TabIndex value.

Text

The Text property is used in different ways by many types of controls. As you saw in Chapter 1, the Text value of a form is displayed in its title bar, whereas the Text value of a label is displayed on its face. Controls such as TextBox will display the Text value as an input value that may be changed.

The Text property can also be used to provide keyboard shortcuts. You simply place an ampersand character (&) in front of a text character to use that character as the access key for the control. For example, a button with a Text value of &Save will display the S in Save as underlined and will accept Alt+S to cause the control to receive the focus. Some controls, such as the Label control, cannot receive the focus. If an access key is assigned to a control such as this, the focus will be received by the control with the next highest TabIndex value.

graphics/note_icon.gif

In order to display an ampersand within the Text value, as in "Texas A&M University," you would include a double ampersand (&&), which tells the control to display the ampersand value itself.


Visible

The Visible property is a Boolean (True/False) value that determines whether the control is visible to the user. Set to True by default, this value only affects the visibility of the control to the user and does not prevent the control from being manipulated programmatically. If this value is set to False, the control remains visible within the Form designer but will not be visible to the end user.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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