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
Event Procedures
Visual Basic makes it easy to locate event procedure code for controls on forms. Double-click any control to see one of its event procedures. For example, if you double-click the Exit command button, Visual Basic opens the Code window and places the text cursor in the set of lines shown in Listing 2.1.
Example 2.1. The Exit command button's Click event procedure.
1: Private Sub cmdExit_Click() 2: ' Unload the form and terminate application 3: Unload frmInterest 4: End 5: End Sub
Wrapper lines are the first and last lines of a procedure.
A block is a section of code that goes together as a single unit.
Don't sweat the details, but become familiar with the overall event procedure. Most event procedures begin with the statement Private Sub… and end with End Sub. The Private…End block illustrates the first and last lines of the event procedure. The lines between these wrapper lines compose the body of the event procedure.
All controls have unique names as you saw earlier. All event procedures also have unique names. An event procedure name always takes this form:
controlName_eventName ()
The event procedure always consists of the control name, an underscore, and the procedure's event name. If you want to respond to both the click and double-click events that might be applied to the Exit command button, you would have to write an event procedure named cmdExit_Click() and one named cmdExit_DblClick().
You don't have to memorize that the double-click event is named DblClick and that a keypress event is named KeyDown. The top of every Code window contains a drop-down list box, which contains every event possible for the control listed in the left-hand drop-down list box. The left-hand list box holds the name of every control on the form. Again, don't get too bogged down in details because when it's time to use these drop-down list boxes to select events, this lesson describes the process in detail.
The naming convention for the event procedure isn't your decision, but Visual Basic's. The Click event procedure for a command button named cmdTest will always have to be cmdTest_Click(). The two-part name makes the event procedure extremely specific; from the name, both you and Visual Basic know that the code executes only if the user clicks the cmdTest command button.
Properties and Event Procedures | Next Section

Account Sign In
View your cart