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
Finalizing the Toolbar
Double-click the Toolbar control to add a toolbar to the top of the form. The toolbar will first appear at the top of the form, which is where most toolbars reside. You can change the Align property if you want to place the toolbar against another edge of the form. Change the toolbar's Name property to tlbNew.
Click the toolbar's Custom property to display the toolbar's Property Pages dialog box, which is shown in Figure 19.6. Although you can set most of the dialog box's properties from the Properties window, you'll find that the Property Pages dialog box makes setting up the toolbox simpler.
Figure 19.6 The Toolbar control's Property Pages dialog box.
To connect the image list to the toolbar, open the ImageList drop-down list box and select imlToolBar (if other image lists appeared on the form, they would all appear and you could select the one that goes with the toolbar). Select the 1-ccFixedSingle BorderStyle property to help distinguish the toolbar from the rest of the form's controls.
To add the toolbar buttons, click the Buttons tab to display the Buttons page. For each button, click Insert Button and change the Image value to 1 (the first image's Index property value). Also type Save for the Key value. When you click Apply (to apply the property values), the first toolbar button will appear with the disk icon that appears first in the image list.
Continue clicking the Insert Button command button and updating the Image text box. Use the following values for the last four Key values: Button, Mouse, Trash, and Stop. For each Key value, increment its Image value by one. When you finish the toolbar buttons, close the dialog box, and the five toolbar buttons with their corresponding icons from the Image List control will appear (see Figure 19.7).
Figure 19.7 The toolbar is now complete.
Run the application and try the new toolbar. When you click a button, you'll see the button clicking. Now you need to hook up the commands to the buttons. Stop the running application to add the event procedure.
The toolbar acts like a control array. To add code that responds to a toolbar's button click, double-click the Toolbar control to open a new event procedure. The first line appears here:
Private Sub tlbNew_ButtonClick(ByVal Button As ComctlLib.Button)
The ButtonClick() event is the toolbar's event that occurs when the user clicks a toolbar button. The argument tells your code which button the user clicked so the code can respond accordingly. You must use the argument's Key method to determine the button clicked. The button's Key method returns the string you entered for the toolbar button's Key method. The following code shows an outline of the code you could write that would execute a different procedure depending on the user's toolbar button click:
Private Sub tlbNew_ButtonClick(ByVal Button As ComctlLib.Button)
' Respond to button clicks
Dim msgPress As Integer
' Display a message box depending
' on which toolbar button the user clicks
Select Case Button.Key
Case Is = "Save":
msgPress = MsgBox("You pressed Save", , "Save")
Case Is = "Button":
msgPress = MsgBox("You pressed Button", , "Button")
Case Is = "Mouse":
msgPress = MsgBox("You pressed Mouse", , "Mouse")
Case Is = "Trash":
msgPress = MsgBox("You pressed Trash", , "Trash")
Case Is = "Stop":
Unload Me
End
End Select
Of course, your application would do more than display a message box when the user clicks a toolbar button. More likely you would insert a Call statement to call a procedure that handles the toolbar button. If the toolbar's buttons mimic menu selections, as most users design toolbar buttons to do, the Call statement can call the corresponding menu item, such as Call mnuFileExit_Click.
The Line and Shape Controls | Next Section

Account Sign In
View your cart