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
Take a Break!
In this section, you are going to create a project from scratch without the help of the VB Application Wizard. You'll create a new project, assign controls, and write event procedure code to hook everything together. The final application will be simple, but you'll have little trouble understanding the application now that you've become more familiar with properties and event procedures.
To create your first application, follow these steps:
- Create a new project by selecting File | New Project and choosing to create a new Standard EXE project. Don't save any changes from earlier in this lesson if you were following along during the discussion of command buttons and control placement.
- Change the form's Name property to frmFirst and change its Caption property to My First Application. The form's Caption property text appears in the title bar when you run the application.
- Expand the Form window to these property values: Height 7380 and Width 7095. You can either drag the Form window's sizing handles until the Form window's size coordinates to the right of the toolbar read 7095x7380 or you can set these two property values yourself by changing the values in the Properties window. If you drag the Form window to obtain this size, you can approximate the coordinates described here; you don't have to size your Form window exactly to 7,095 by 7,380 twips.
- Click the Label control once. As you learned in Hour 1, "Visual Basic at Work," the Label control is the tool with the capital letter A on the toolbox. When you click the Label control, Visual Basic shows the control depressed as if it were a command button.
- Move the mouse pointer onto the Form window and drag a Label control toward the top of the Form window in the approximate location you see in Figure 3.9.
Figure 3.9 A label is this form's first control.
- Change the label's Name property to lblFirst. Change the label's Caption property to VB is fun.
- Click the label's Font property value to display the ellipsis. Click the ellipsis to display the Font dialog box for the label. Set the font size to 24 points and set the Bold property. (A point is 1/72 of an inch; 24 points is about twice the height of a word processor's character onscreen.)
As Figure 3.10 shows, the label's text is now large enough to read, but the text isn't well centered within the label. Change the label's Alignment property to 2-Center, and the text centers just fine.
Figure 3.10 The label needds to be centered.
- Change the label's BorderStyle property to 1-FixedSingle. This property adds a single-line 3D border around the label. You'll see that the label's Height property is too large, so click the label to display its sizing handles and drag the top edge downward to center the text within the label.
- Add a command button, but to do so, double-click the command button tool on the Toolbox window. The command button appears in the middle of the form and you can leave it where it is.
- Change the command button's Name property to cmdExit. Change the command button's Caption property to E&xit. Watch the command button as you type the Caption property text. The command button's caption becomes the text you type with one exception: The x is underlined. When you precede a Caption property's letter with an ampersand (&), Visual Basic uses that letter for the control's hotkey. Users of your application will be able to select the command button not only by clicking the mouse, but also by pressing Alt+X on the keyboard.
- The command button will be used to exit the program. When the user clicks the command button, your application should end. What happens when a user clicks a command button? A Click event occurs. Therefore, to respond to this event, you must write an event procedure for the command button. Visual Basic will help you do this. Double-click the form's command button and Visual Basic instantly opens the Code window and displays the following wrapper lines for the command button's Click event procedure:
Private Sub cmdExit_Click() End Sub
You only need to fill in the body. The name of the procedure, cmdExit_Click(), describes both the control and the event being processed by the code. Type End for the one-word body of the event procedure and close the Code window. End is now the very first Visual Basic programming language statement you've learned! End tells Visual Basic to terminate the running application, so the application will terminate when the user clicks the command button.
Press F5 to run the program and watch your creation appear. As shown in Figure 3.11, the form appears with the label and command button in place.
Figure 3.11 Your first running application!
Terminate the application by clicking the Exit command button. Visual Basic regains control. (If you had compiled the application, you could run the compiled .exe file from the Windows Run command or from an icon if you assign the .exe file to an icon on the Desktop or to an option on the Start menu.)
When you save the project, Visual Basic saves all the files within the project. Select File | Save Project. Visual Basic asks for the form's name with a Save File As dialog box (remember that each element of the project is a separate file). You can select a different drive or pathname if you want. Save the form module file under the name Lesson 3 Form (Visual Basic automatically adds the .frm filename extension). Visual Basic now requests the name of the project with a Save Project As dialog box. Type Lesson 3 Proj and click Save to save the project file (Visual Basic automatically adds the .vbp filename extension). If you were to edit the project, Visual Basic would not need to request the filenames subsequently now that you've assigned them.
Take a rest before starting Hour 4, "Examining Labels, Buttons, and Text Boxes." Exit Visual Basic and give your computer's circuits a break as well. You are well on your way to becoming a Visual Basic guru, so feel good about the knowledge you've already gained in three short hours.
Summary | Next Section

Account Sign In
View your cart