Visual Basic in 12 Easy Lessons vel06.htm

Previous Page TOC Next Page



Lesson 3, Unit 6


Polishing Forms and Controls



What You'll Learn


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.

Properties of the Form


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.

Table 6.1. Properties of the form.

Property Description
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 event—a redrawing event triggered when graphic images are covered and then uncovered—redraws 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.


Definition: A pixel is the smallest screen width possible on your monitor.

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.
FontBold 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.
FontItalic 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.
FontName 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.
FontSize 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.
FontStrikethru 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.
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.
FontUnderline 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.


Definition: MDI stands for Multiple Document Interface.

MDIChild If True, the form is a MDI form—that is, a child form within a parent form. If False (the default), the form is not a 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.
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.
Picture A picture file that displays 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.
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.
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.

Table 6.2. The ScaleMode property values.

Value Description
0 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.

Advanced Labels


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:



Definition: Truncate means to chop off.

  1. The text might not fit inside the label, and Visual Basic truncates the text. Figure 6.1 shows the result.
    Set the AutoSize property to False if you want the label to remain the same size. The application assigns the text, and the label might not hold the entire caption.

    Figure 6.1. The label is not large enough.

  2. The label automatically expands downward to hold the entire caption in a multiline box. Figure 6.2 shows the result.
    Set both the AutoSize and WordWrap properties to True if you want the label to expand vertically to hold the entire caption that you assign at design time and during execution.

    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.

  3. The label automatically expands across the screen to hold the entire caption in a long label control. Figure 6.3 shows the result.
    A long label like this is not necessarily incorrect. Depending on the length of the text that you assign to the label during the program's execution, there might be plenty of screen space to display long labels. To automatically expand the label horizontally, set the AutoSize property to True and leave WordWrap set to False. This is the default setting.

Figure 6.3. The label is not tall enough.

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.

Scrolling Text Boxes


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 must also set the Scrollbars property to something other that 0-None. The user has to 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 enables 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 label's scroll bars give the user more data-entry freedom.



Note: You cannot enter an initial default value for a multiline text box. You can only initialize a text box with text that spans more than one line in the text box at runtime.

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 by accepting more than one line of text.

Using Focus to Control Text Boxes


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 application shown in Figure 6.6 is identical to the application 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 access 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, access 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+X—the access 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.

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 application in which each label has an access 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 access keys to text boxes, you can add access keys to the labels that describe text boxes. By doing so, you give your users shortcut access to any text box on the form.

Introducing Control Events


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. 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 appears on 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.


Table 6.3. Form events.

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.
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.
Unload Occurs when the form is unloaded using the Unload statement.


Warning: Don't 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.

Table 6.4. Command button events.

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.

Table 6.5 describes the events available for the label controls that you place on forms.

Table 6.5. Label control events.

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.

Table 6.6. Text box control events.

Event Description
Change Occurs when the text box's Text property changes.
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.

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.

Homework



General Knowledge


  1. Is the form an object?

  2. True or false: Label controls can have more than one line of text as long as you set the MultiLine property.

  3. What is a pixel?

  4. Which form property determines how the form appears when the user first sees the form?

  5. What does truncate mean?

  6. True or false: Suppose that you want to store a lot of text in a label. You must make the label large enough to hold the entire text if the user is to see all of it.

  7. True or false: You can add both horizontal and vertical scroll bars to a label.

  8. How can you ensure that a label does not expand to display a long value?

  9. True or false: You can enter an initial multiline text value by using the Properties window.

  10. True or false: A label can get the focus as long as you supply an access shortcut key for the label.

  11. Which property determines the focus order?

  12. What is perhaps the most important form event?

  13. Which form event occurs first: Load or Activate?

  14. What window can you use to see all the events for objects?

  15. Why is there no GotFocus event for labels?


Write Code That...


  1. Suppose that you were writing a Change event procedure for a text box label called txtLastName. What you name the event procedure?

  2. When the user presses a key, either the form gets the keystroke or a control gets the keystroke. What property determines which object gets the keystroke?

  3. Suppose that you need to change a form's title in the form's title bar. Which property do you change—the Caption property or the Name property?

  4. Describe how you add access keystrokes to text boxes.


Find the Bug


  1. Why should you avoid putting too many self-sizing labels on the form at one time?

  2. What happens if you set the AutoSize property to True before you set the WordWrap property to True?


Extra Credit


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?

Previous Page Page Top TOC Next Page