Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
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).
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.
Figure 9.3 Proper form design takes awhile.
Finishing the Form | Next Section

Account Sign In
View your cart