Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Using Control Arrays

This is a great time to introduce a new concept called control arrays. A control array is a group of more than one control of the same control type. You will better understand control arrays after you learn about data arrays in Hour 10, "List Boxes and Data Lists," but the interest rate application makes this a great project to introduce them.

Notice that the interest calculator's Term and Investment Amount labels and text boxes look similar to the Interest rate label and text box you just placed on the form; the font information and Height properties are all the same. Therefore, although you could enter the remaining labels and text boxes, you can utilize the Windows Clipboard to make the job go faster.

Highlight both the existing Interest rate label and text box. You can select multiple controls by pressing the Ctrl key while you click each control, or you can lasso the controls by dragging a rectangle around the two controls. When you select both controls, sizing handles appear around them (see Figure 9.2).

09fig02.jpg

Figure 9.2 The sizing handles show that two controls are selected.

Select Edit | Copy to copy the selected controls to the Windows Clipboard. Now select Edit | Paste, and Visual Basic pops up a warning dialog box that says You already have a control named 'lblRate'. Do you want to create a control array?

A control array is a set of multiple controls that have the same name. You distinguish between the controls inside the array with an index value. For this particular example, you should not create a control array (you will learn how to create one in the next lesson). Answer No to the dialog box and answer No again when Visual Basic asks you about creating a control array for the text box.

Move the pasted label and text box to their correct positions under the first pair and set these properties for the label:

   Name:                         lblTerm
   
   Caption:                      &Term (annual periods):
   
   Left:                         2040
   
   Top:                          1800
   
   Width:                        2895

The Height and Font properties are already correct because you borrowed these properties from the control you originally copied from. Set these properties for the text box:

   Name:                        txtTerm
   
   Left:                        5160
   
   ToolTipText:                 Number of periods
   
   Top:                         1800
   
   Width:                       615

As you can see, you don't have to set as many properties when you paste from an existing similar control.

Default properties are the properties Visual Basic assumes if you don't set the properties at design time or within the application's code at runtime.

Click the form and select Edit | Paste once more (the Clipboard still holds those first two controls you sent there). Refuse the control array and set the following properties for the new label:

   Name:                      lblInvest
   
   Caption:                   I&nvestment Amount:
   
   Left:                      1920
   
   Top:                       2520
   
   Width:                     2895

Set the text box's properties to the following:

   Name:                      txtInvest
   
   Left:                      5160
   
   ToolTipText:               Money you invested
   
   Top:                       2520
   
   Width:                     1215

Your screen should look similar to Figure 9.3.

09fig03.gif

Figure 9.3 Proper form design takes awhile.

Share ThisShare This

Informit Network