This unit continues the work begun in Unit 5. It explains more about the form and its property settings. In addition, this unit delves further into label and text box controls by showing you some of the more advanced uses of those controls. Not only will you learn more about the property settings, but you will also learn what events are possible for these controls.
By the time you complete this unit, you will know virtually everything there is to know about forms, command buttons, labels, and text boxes. The project at the end of this lesson demonstrates these controls in action. It will give you a better sense of the ways in which you can implement them.
Concept: The form is yet another Visual Basic object. As such, it has property settings that you can set and change while you design the application and during the program's execution. This section explains all the form's property settings in detail.
Table 6.1 describes the property settings of the form that appear in the Properties window when you click the Form window and press F4. The form has more properties than the command button, label, and text box controls, whose properties you saw in the previous unit. As with all control property values, you never need to worry about all these properties at once. Most of the time, the default values are satisfactory for your applications.
| Property | Description |
| Appearance | If 3D, the form takes on more of a three-dimensional appearance when pressed. If Flat, the form retains a flatter style. |
| AutoRedraw | If True, Visual Basic automatically redraws graphic images that reside on the form when another window hides the image or when the user resizes the object. If False (the default), Visual Basic does not automatically redraw. |
| BackColor | The background color of the form. You can enter a hexadecimal Windows color value or select from the color palette. |
| BorderStyle | Set to 0 for no border or border elements such as a control menu or minimize and maximize buttons, 1 for a fixed-size border, 2 (the default) for a sizable border, or 3 for a fixed-size border that includes a double-size edge. |
| Caption | The text that appears in the form's title bar. The default Caption is the Name of the form. |
| ClipControls | If True (the default), the Paint eventa redrawing event triggered when graphic images are covered and then uncoveredredraws the graphics. If False, only newly-exposed areas of the graphics are repainted. |
| ControlBox | If True (the default), the form contains a control button and control menu. If False, the form does not contain a control button and a control menu. |
| DrawMode | Contains 16 advanced settings that interact with drawing properties to produce special drawing effects. (See Lesson 11 for more information on graphics.) |
| DrawStyle | Contains seven advanced settings that determine the appearance of lines that you draw. |
DefinitionA pixel is the smallest possible part of an image on your screen.
| DrawWidth | The width, in pixels, of lines drawn on the form. |
| Enabled | If True (the default), the form can respond to events. Otherwise, Visual Basic halts event processing for the form. |
| FillColor | The color value used to fill shapes drawn on the form. |
| FillStyle | Contains eight styles that determine the appearance of the interior patterns of shapes drawn on the form. |
| Font | When you click the Font's ellipses, Visual Basic displays a font dialog box with which you can set the characteristics of the characters that appear on the form. For example, you can select the font's name, style, size in points, and the underline status. |
| FontTransparent | Has no effect on the form's Caption property, but does affect text that you eventually display on the form if you use the Print command. |
| ForeColor | The color of foreground text that you display on the form if you use the Print command. |
| Height | The form's height in twips. |
| HelpContextID | Provides the identifying number for the help text if you add advanced context-sensitive help to your application. |
| Icon | The picture icon that the user sees after minimizing the form. |
| KeyPreview | If False (the default), the control with the focus receives these events: KeyDown, KeyUp, and KeyPress before the form does . If True, the form receives the events before the focused control. |
| Left | The number of twips from the left edge of the screen to the left edge of the form. |
| LinkMode | Set to 0 (the default) for no DDE allowance, 1 for automatic DDE allowance, 2 for a code-based DDE, or 3 for a code-based notify requirement. |
| LinkTopic | Specifies the source application and topic for a DDE application. |
| MaxButton | If True (the default), the maximize button appears on the form at runtime. If False, the user cannot maximize the form window. |
DefinitionMDI stands for Multiple Document Interface.
| MDIChild | If True, the form is an MDI formthat is, a child form within a parent form. If False (the default), the form is not an MDI form. |
| MinButton | If True (the default), the minimize button appears on the form at runtime. If False, the user cannot minimize the form window. |
| MouseIcon | Lets you specify a picture file used for the mouse icon. |
| MousePointer | The shape to which the mouse cursor changes if the user moves the mouse cursor over the form. The possible values range from 0 to 12 and represent the different shapes the mouse cursor can take on. (See Lesson 12.) |
| Name | The name of the form. By default, Visual Basic generates the name Form1. |
| NegotiateMenus | If True, the form's menu deactivates and another object's menus take over. If False, the form's menu is active. |
| Picture | A picture file that is displayed on the form's background. |
| ScaleHeight | The height of the form. ScaleMode determines the unit of measurement used. |
| ScaleLeft | The distance from the left of the screen to the left edge of the form. ScaleMode determines the unit of measurement used. |
| ScaleMode | Enables you to determine how to measure coordinates on the form. You can choose from eight values. The default unit of measurement is twips, indicated by 1. The other Scale... properties measure use twips. Table 6.2 describes the possible units of measurement. |
| ScaleTop | The distance from the top of the screen to the top edge of the form. ScaleMode determines the unit of measurement used. |
| ScaleWidth | The width of the form. ScaleMode determines the unit of measurement used. |
| ShowInTaskbar | If True, the form appears on the Windows taskbar. |
| Tag | Not used by Visual Basic. The programmer can use it for identifying comments applied to the form. |
| Top | The number of twips from the top edge of the screen to the top of the form. |
| Visible | True or False, indicating whether the user can see and, therefore, use the form. |
| WhatsThisButton | If True, the What's This? Help button appears in the form's upper-right corner. |
| WhatsThisHelpID | Assigns a specific help number to the command button so that when the user clicks the What's This help button (the question mark in the upper-right corner of some windows) and then clicks the command button, the ID specifies exactly which help message appears. |
| Width | The width of the form in twips. |
| WindowState | Describes the startup state of the form when the user runs the program. If set to 0 (the default), the form first appears the same size as you designed it. If set to 1, the form first appears minimized. If set to 2, the form first appears maximized. |
ScaleMode enables you to determine how to measure coordinates on the form. You can choose from eight values. The default unit of measurement is twips. Table 6.2 describes the possible units of measurement.
| Value | Description |
| 0 | User Customized values |
| 1 | Twips (the default) |
| 2 | Points |
| 3 | Pixels |
| 4 | A standard character that is 120 twips wide and 240 twips high |
| 5 | Inches |
| 6 | Millimeters |
| 7 | Centimeters |
Review: You can customize your form in all kinds of ways. You can make it appear maximized or minimized. You can use colors and various styles. Most of the time, you want just a simple form with a caption that identifies the application; the only property values that you probably will have to modify are the Caption properties.
Concept: In the previous unit, you saw all the property values that you can set with labels. Some of the property values produce interesting effects, which are described in this section.
Suppose that you design a label that contains this long caption:
If a label's caption is too lengthy, you will need to adjust the label some way.
A label is rarely wide enough or tall enough to hold this caption. If you attempt to type text into a label's Caption property that is longer than what fits the size of the label, one of the following things can take place depending on how you have set up the label:
Figure 6.2. The label resizes vertically.
Tip: Set WordWrap to True before you set the AutoSize property to True. If you set AutoSize first, the label expands horizontally before you have a chance to set the WordWrap property.
Warning: Be careful about placing too many automatically-resizing labels. The labels might overwrite important information on the form if their captions are too long.
Figure 6.3. The label resizes horizontally.
Review: Putting captions in labels seems easy until you think about the effects that can occur if the label is too large or too small to hold the text. By using the property combinations described here, you can add automatically-adjusting labels for whatever text the labels need to hold.
Concept: By adding scroll bars to text boxes, you can give the user multiline text box capabilities. That way, the user can enter and edit long lengths of text without running out of room inside the text boxes.
The MultiLine property for text box controls determines whether or not the text box can contain one or more lines of text. The multiline text might be an initial default value that you store in the text box's Text property when you place the form on the control. The multiline text also might consist of the user's input when the program runs.
Warning: If you set the MultiLine property to True, you can also set the Scrollbars property to something other than 0-None. The user will then have a way to see the multiple lines of text inside the text box and the scroll bars give the user that ability.
Figure 6.4 shows a text box that contains scroll bars and a True value for the MultiLine property. As the user enters text in a box like this, he can press Enter to move to the next line in the box. He does not, however, have to press Enter just because the text happens to scroll to the right; the horizontal scroll bars enable him to scroll left and right. When the user wants to end each line in the text box though, he presses Enter to move the carriage return character to the next line.
Figure 6.4. The text box's scroll bars give the user more data-entry freedom.
Review: By setting the MultiLine and the ScrollBars properties, you can use multiline text boxes in your applications. Multiline text boxes respond to user input and accept lengthy user inputs and can display lengthy user outputs.
Concept: Although access keys are not available for text boxes, you can use a little-known trick to supply access keystroke shortcuts for text box data entry.
As you begin to build Visual Basic applications, you will use text box controls to capture user input. Don't just throw a text box on a form and expect the user to know what to enter in it. In Figure 6.5, for example, the user does not know what data he should enter in the text box controls.
Figure 6.5. Text boxes without labels confuse the user.
You must label the text box with a label control that tells the user what you want. The form shown in Figure 6.6 is identical to the form shown in Figure 6.5, but the labels in front of each text box tell the user what kind of data to enter.
Figure 6.6. The user now knows what data is expected.
Tip: Add shortcut keystrokes to labels that describe data-entry text boxes.
Suppose that the user fills in the six sales figures. He might want to go back and change an entry to fix a typing error. When you put text boxes in an application, think about giving your users a chance to correct their mistakes by providing them an access keystroke to each text box.
As you know, shortcut keys are the Alt+Keystroke combinations that you can apply to controls such as command buttons. In Figure 6.6, the user can press Tab to the Exit command button, click the Exit command button with the mouse, or press Alt+Xthe shortcut key for the command button. The underline indicates which letter provides the access.
Text box controls do not have captions, so you cannot directly add access keystrokes to text boxes. Nevertheless, you can add underlined access keystrokes to label captions. For example, suppose that you changed the first label in Figure 6.6 from January to January. The access keystroke for that label is Alt+J.
But wait, there's a problem. Labels cannot receive the focus! If a label contains an access keystroke and the user presses that access keystroke combination, Visual Basic knows that the focus cannot go to the label. It sends the focus to the next control in the TabIndex sequence. All controls contain a TabIndex property. A different numeric value appears in each control's TabIndex property. As you learned in the previous unit, the TabIndex property determines the focus order. Suppose that you assigned a TabIndex value of 0 to the January label and a TabIndex value of 1 to the text box to the right of January. When the user presses Alt+J, the focus goes to the text box because the label cannot receive a focus.
Warning: Make sure that each label's UseMnemonic property is set to True to allow the label's shortcut key to work. Visual Basic sets the label's UseMnemonic value to True by default.
Therefore, after you place all the controls on a form and set their properties, go back to each control and make sure that each label contains a TabIndex value that is one less than the text box control that the label describes. Make sure, as well, that the overall TabIndex sequence is organized so that it sends the focus from control to control in the order you want as the user presses Tab. Figure 6.7 shows the six-month sales data-entry form in which each label has an shortcut keystroke. The figure indicates the TabIndex value for each control. Given the TabIndex values, the focus goes directly to the May text box when the user presses Alt+Y.
Figure 6.7. The TabIndex properties describe the access keystroke order.
Review: Although you cannot add shortcut keys to text boxes, you can add shortcut keys to the labels that describe text boxes. By doing so, you give your users shortcut to any text box on the form.
Concept: You know that when the user clicks command buttons and types text in text boxes, he triggers events that your program can capture. This section discusses the events available for the command button, label, and text box controls. Lesson 4 begins your study of the Visual Basic programming language, so you need to understand which events are possible as you write event procedures that respond to those events.
Here's another section full of tables! Nevertheless, they show you all the events available for the controls you have been learning. In the next lesson, you will begin to write code. The code that you write usually appears inside event procedures. You need to know which events are available, so that you can write the correct event procedures.
Note: As you learn new controls in subsequent lessons, you will find more tables properties and events. Enjoy.
Table 6.3 describes the events related to forms. Perhaps the most important form event is Load, which triggers whenever the user runs an application and right before the form loads. Throughout this book, you will use the Load event to put startup code in applications so that the startup code executes immediately after the user runs the application and immediately before the form loads onto the screen.
Tip: Remember that if you want to see what events are possible for a certain control, place the control on the Form window and double-click the control. Visual Basic opens a Code window. Open the Proc: dropdown combo box list to see a list of the events available for that control.
| Event | Description |
| Activate | Occurs when a form becomes the active window. In Visual Basic, the Activate event occurs after the Load event displays the form. |
| Click | Occurs when the user clicks the form with the mouse. |
| DblClick | Occurs when the user double-clicks the form with the mouse. |
| Deactivate | Occurs when another form becomes the active window. Not available in the Visual Basic Primer Edition. |
| DragDrop | Occurs when a drag operation over the form completes. |
| DragOver | Occurs during a drag operation over the form. |
| GotFocus | Occurs when the form receives the focus. |
| Initialize | Occurs when Visual Basic first creates the form. |
| KeyDown | Occurs when the user presses a key and the KeyPreview property for the controls on the form is set to True. Otherwise, the control gets the KeyDown event. |
| KeyPress | Occurs when the user presses a key over the form. |
| KeyUp | Occurs when the user releases a key. |
| LinkClose | Occurs when a DDE operation terminates. |
| LinkError | Occurs when a DDE operation fails. |
| LinkExecute | Occurs when a DDE operation begins to execute. |
| LinkOpen | Occurs when a DDE operation begins. |
| Load | Occurs when the form loads and before it appears on the screen. |
| LostFocus | Occurs when the form loses the focus. |
| MouseDown | Occurs when the user presses the mouse button over the form. |
| MouseMove | Occurs when the user moves the mouse over the form. |
| MouseUp | Occurs when the user releases the mouse over the form. |
| Paint | Occurs when Visual Basic must redraw a form because another object overwrote part of the form and then the user moved the object and exposed the hidden part of the form. |
| QueryUnload | Occurs immediately before the application terminates. |
| Resize | Occurs when the user resizes the form. |
| Terminate | Occurs when the form goes away (such as when the user closes the form window). |
| Unload | Occurs when the form is unloaded using the Unload statement. |
Warning: Don't let Table 6.3 scare you away from Visual Basic! You will use only a handful of these events in most of your programming work.
Notice that all the descriptions in Table 6.3 begin with the word occurs. Each of these table entries are events that occur as the result of a user or Windows action. Therefore, if you want to do something when the user clicks the form, you write code that performs the task that you want accomplished and you put that code inside the form's Click event procedure. If the form is named MyForm, the Click event procedure is named MyForm_Click(), as you learned in the Unit 4. You will start writing the code for event procedures in the next lesson.
Table 6.4 describes the events available for the command button controls that you place on forms.
| Event | Description |
| Click | Occurs when the user clicks the command button with the mouse. |
| DragDrop | Occurs when a drag operation of the command button completes. |
| DragOver | Occurs during a drag operation of the command button. |
| GotFocus | Occurs when the command button receives the focus. |
| KeyDown | Occurs when the user presses a key and the KeyPreview property for any control on the form is set to False. Otherwise, the form gets the KeyDown event. |
| KeyPress | Occurs when the user presses a key over the command button. |
| KeyUp | Occurs when the user releases a key. |
| LostFocus | Occurs when the command button loses the focus to another control or to the form. |
| MouseDown | Occurs when the user presses the mouse button over the command button. |
| MouseMove | Occurs when the user moves the mouse over the command button. |
| MouseUp | Occurs when the user releases the mouse over the command button. |
Table 6.5 describes the events available for the label controls that you place on forms.
| Event | Description |
| Change | Occurs when the label's Caption property changes. |
| Click | Occurs when the user clicks the label with the mouse. |
| DblClick | Occurs when the user double-clicks the label with the mouse. |
| DragDrop | Occurs when a drag operation of the label completes. |
| DragOver | Occurs during a drag operation of the label. |
| LinkClose | Occurs when a DDE operation terminates. |
| LinkError | Occurs when a DDE operation fails. |
| LinkNotify | Occurs when a DDE operation notifies the label with a changed message. |
| LinkOpen | Occurs when a DDE operation begins. |
| MouseDown | Occurs when the user presses the mouse button over the label. |
| MouseMove | Occurs when the user moves the mouse over the label. |
| MouseUp | Occurs when the user releases the mouse over the label. |
Note that no GotFocus event is associated with labels. This is because a label can never receive the focus.
Table 6.6 describes the events available for the text box controls that you place on forms.
| Event | Description |
| Change | Occurs when the text box's Text property changes. |
| Click | Occurs when the user uses the mouse to click the text box. |
| DblClick | Occurs when the user uses the mouse to double-click the text box. |
| DragDrop | Occurs when a drag operation of the text box completes. |
| DragOver | Occurs during a drag operation of the text box. |
| GotFocus | Occurs when the text box receives the focus. |
| KeyDown | Occurs when the user presses a key and the KeyPreview property for the controls on the form is set to True. Otherwise, the form gets the KeyDown event. |
| KeyPress | Occurs when the user presses a key over the text box. |
| KeyUp | Occurs when the user releases a key over the text box. |
| LinkClose | Occurs when a DDE operation terminates. |
| LinkError | Occurs when a DDE operation fails. |
| LinkNotify | Occurs when a DDE operation notifies the text box with a changed message. |
| LinkOpen | Occurs when a DDE operation begins. |
| LostFocus | Occurs when the text box loses the focus to another object. |
| MouseDown | Occurs when the user presses the mouse button over the label. |
| MouseMove | Occurs when the user moves the mouse over the label. |
| MouseUp | Occurs when the user releases the mouse over the label. |
Review: Each control has its own set of properties and command buttons. The tables in this unit complete the discussion of forms, command buttons, labels, and text boxes. You now know enough to work with these four fundamental Visual Basic objects. The next lesson builds on your knowledge by teaching you how to add code to event procedures.
What if you wanted to enter Property window settings in inches instead of twips, the default unit of measurement. How do you change the unit measurement to inches?