Visual Basic in 12 Easy Lessons velp03.htm

Previous Page TOC Next Page



Project 3


Getting Down to Business



Stop & Type


In this lesson, you learned about all the properties and events that are possible with the form, label, text box, and command button objects. Visual Basic can handle virtually all the requirements that your application will need.

In this lesson, you saw the following:

The project named PROJECT3.MAK contains this lesson's project. You should load PROJECT3.MAK and run the application so that you can follow along with the description of this project.

The Program's Description


Figure P3.1 shows how the PROJECT3.MAK application looks as soon as you run the program. The project contains the following objects:

Notice that the top label contains an access key, Alt+T, that will send the focus to the text box.

Figure P3.1. The project's opening screen.

The purpose of this program is to request a message that you can enter in the text box. You can type a message as long or as short as you want. After you type the message, click the center command button or press Alt+S to send that text box message to the empty label below the center command button.

The label receiving the text box data resizes automatically to adjust to the size of the text. Therefore, if you type only a single word, the label shrinks to fit around that one word. If, instead, you type several lines of text, such as a poem, all of the lines of text appear in the label because the label grows to hold all the text.

The Program's Action


Now type the words Visual Basic in the text box. Press Alt+S to send the contents of the text box to the label at the bottom of the screen. The label shrinks to hold the two words, as shown in Figure P3.2.

Figure P3.2. Watch how the label resizes.

Obviously, the command button triggers an event that takes all the text in the text box and sends a copy of it to the label at the bottom of the screen. You have yet to learn Visual Basic coding commands, so I'll save the discussion of event procedure code for the next unit.

The resizing label at the bottom of the screen contains the following Property window settings:

If the AutoSize property is not set to True, the label does not automatically shrink to fit the small text box value assigned to the label. Likewise, when you enter a larger message in the text box and press Alt+S to send it to the lower label, the label does not grow to accommodate the larger message. The WordWrap value of True ensures that the label grows vertically instead of expanding horizontally as a long, one-line label.

Try a different message. Press Alt+T to send the focus back to the text box. Press the Backspace key to erase the two words currently in the text box. Enter the following beautiful poem written several hundred years ago by a composer of lovely symphonies. (Okay, so I wrote the poem, but it is beautiful if you ask me.)

Visual Basic is like music,
ringing in my ears.
When I think of the things it does,
my eyes fill up with tears.

Press Alt+S to send the poem to the label. See how fast the label expands vertically to hold the full message. Figure P3.3 shows what your screen should look like.

Figure P3.3. The label grows as large as necessary.

***Begin Note***



Note: If you pressed Enter at the end of each poem line, your screen will match the one shown in Figure P3.3. Otherwise, your poem will be squeezed together a bit more than how it appears in Figure P3.3.

***End Note***

***Begin Warning***

Warning: Visual Basic is extremely sensitive. If you cringe when you read the author's poetry, Visual Basic will refuse to work for you ever again.

***End Warning***

Close the Application


Until this lesson, you would not have been able to place a label that resized at runtime to fit automatically whatever text it received. Look again at the screen. You will see another trick taught by this lesson: You can send the focus to the text box by pressing the access key (Alt+T) for the label just above the text box. The only way to send the focus to the text box is to ensure that the TabIndex property for the label is one less than the TabIndex property for the text box. When Visual Basic realizes that the access key sends the focus to a label that cannot receive the focus, Visual Basic sends the focus to the next control as determined by the order of the objects' TabIndex values.

You can now exit the application and exit Visual Basic. Take a rest because you will begin writing your first Visual Basic textual code in the next unit.

Previous Page Page Top TOC Next Page