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
Program Objects
A class is a packaged object, with behaviors and properties that describe members of the class.
Objects that you create with your application are objects that are members of a particular class. For example, an option button class defines properties, events, and methods that all members of the option button class support. In other words, even though your application may contain five option buttons, and even though all five of those option buttons differ in one or more of their properties (such as Caption), they are all members of the same class. A command button can never be a member of the option button class because a command button's properties, events, and methods differ from an option button's.
You can test for membership within any given class. The class forms a hierarchy and all members of the class take on the class properties, events, and methods. One of the reasons for a class test is that you can pass to procedures not only variables, but also controls. The following procedure receives a command button as its only argument:
Public Sub GetIt(cmdClick As CommandButton)
Some procedures can be multipurpose. In other words, a procedure might change the BackColor property of whatever object you pass to that procedure. Use the As Object argument declaration as follows to make the procedure multipurpose:
Public Sub ChangeColor(objOnForm As Object)
You haven't seen the Object keyword until now, but you can declare not only arguments as Object datatypes, but variables as well, like this:
Dim objAnything As Object
The objAnything variable can now represent an object.
Your application's code can create any object needed at runtime. In other words, you could declare an array of five option buttons like this:
Dim ctlOpButtons(1 To 5) As New OptionButton
The New keyword tells Visual Basic to create five new option buttons. If you want to base a new object on an existing object, you only need to change the properties that differ in the new object from the old one. The following statement declares a new form based on an existing form named frmAcctsPay:
Dim frmNewForm As New frmAcctsPay
Notice that if you place an existing control name after the New statement, Visual Basic declares a new object based on an existing one. If you use a control's class name (such as CommandButton, Form, OptionButton, or Label), Visual Basic declares a new control with all default property values (except for the Name property, which you set with the Dim statement as you declare the control). You then can specify the property values that you want for your new object.
Use the If TypeOf...Is programming block to test for an object's datatype. The following If generates True if the object stored in objAnything is a text box:
If TypeOf objAnything Is TextBox Then
In addition to being a keyword command, Visual Basic supports the TypeOf() function that returns the object type of its argument.
Knowing about an object's class lets Visual Basic accept the following code that contains a With keyword block:
With lblTitle .Caption = "Accounts Payable" .Alignment = vbRightJustify .Font.Size = 15 .Font.Bold = True .Left = 25 .Right = 0 .Width = 1000 End With
If you must set more than two or three properties in code, use With, which tells Visual Basic that all objects without an object qualifier are label objects. Without the With keyword, you would have to type the object's name all through the assignments, like this:
lblTitle.Caption = "Accounts Payable" lblTitle.Alignment = vbRightJustify lblTitle.Font.Size = 15 lblTitle.Font.Bold = True lblTitle.Left = 25 lblTitle.Right = 0 lblTitle.Width = 1000
Using Collections and Object Arrays | Next Section

Account Sign In
View your cart