Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

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.

19fig06.gif

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).

19fig07.gif

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.

Share ThisShare This

Informit Network