Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


You use the toolbar control. In fact, probably the easiest way to add a toolbar to a program is to design that program with the Visual Basic Application Wizard. We’ll take a look at what the Application Wizard has to offer us, and then add a toolbar to a program ourselves.

When you use the Application Wizard to create a program, that program gets a toolbar automatically. You can arrange and configure the toolbar with the Application Wizard Customize Toolbar dialog box, shown in Figure 15.4, which appears when you create a program with the Application Wizard.


Figure 15.4  The Application Wizard Customize Toolbar dialog box.

The Application Wizard takes care of all the details for us. When you run the program it generates, you see a fully functional toolbar in that program, as shown in Figure 15.5.


Figure 15.5  An Application Wizard program, complete with toolbar.

However, most programmers will want to add their own toolbars to their programs, and you create a toolbar by adding a toolbar control to a form. Here’s how that works:

1.  Select the Project|Components menu item.
2.  Click the Controls tab in the Components dialog box.
3.  Select the Microsoft Windows Common Controls item, and click on OK to close the Components dialog box.

This adds the Toolbar Control tool to the Visual Basic toolbox, as shown in Figure 15.1. To place a toolbar in your form, just double-click the Toolbar Control tool.

Now you’ve got a new toolbar—but how do you align it at the top of the window and add buttons to it? See the next couple of topics in this chapter.

Aligning Toolbars In A Form

Now that you’ve added a toolbar to your form, where does it go? By default, it aligns itself with the top of the client area of the form. You can set the alignment of the toolbar with its Align property, which can take these values:

  vbAlignNone—0
  vbAlignTop—1 (the default)
  vbAlignBottom—2
  vbAlignLeft—3
  vbAlignRight—4

Adding Buttons To A Toolbar

You’ve got your new toolbar in the form you want and aligned it correctly. How about adding some buttons?

You add buttons to a toolbar control at design time by right-clicking the control and clicking the Properties item in the menu that appears. When the toolbar’s property pages open, click the Buttons tab, as shown in Figure 15.6.


Figure 15.6  Adding new buttons to a toolbar.

You insert new buttons by clicking the Insert Button button (and remove them with the Remove Button button). When you add a new button to a toolbar, you can associate a picture or caption with it. For example, to give a button a caption, just fill in the Caption box in Figure 15.6.

Each button gets a new Index value, which will be passed to the Click event handler. You can also give each button a Key value, which is a string that you can use to identify the button.

When you’re done, click on the OK button to close the toolbar’s property pages. Now that you’ve installed buttons in your toolbar, how do you handle button clicks? Take a look at the next topic.

Handling Toolbar Buttons Clicks

Now that you’ve set up your toolbar with the buttons you want, how can you make those buttons active? You do that with the toolbar control’s ButtonClick event:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)

End Sub

The button the user clicked is passed to us in this event handler procedure, and we can determine which button was clicked by checking either the button’s Index or Key properties. For example, we can indicate to users which button they clicked with a message box and the Index property this way:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
    MsgBox "You clicked button " & Button.Index
End Sub

All buttons in a toolbar control have an Index value by default (this value is 1-based), so this code is ready to go. When the user clicks a button, we report which button the user has clicked, as shown in Figure 15.7.


Figure 15.7  Determining which button the user has clicked.

Besides using the Index property, you can also give each button’s Key property a text string (you do that at design time in the toolbar control’s property pages). Then you use a Select Case statement to determine which button was clicked, like this:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "OpenFile"
            OpenFile
        Case "SaveFile"
            SaveFile
        Case "CloseFile"
            CloseFile
    End Select
End Sub

The complete code for the preceding code where we use the Index property appears in the toolbars folder on this book’s accompanying CD-ROM.

Connecting Toolbar Buttons To Menu Items

You often use buttons in a toolbar as shortcuts for menu items. How do you connect a toolbar button to a menu item? You just call the menu item’s Click event handler when the button is clicked.

For example, if you have three items in the File menu, Open, Save, and Close, that you want to connect to toolbar buttons, you can set those buttons’ Key properties to, say, “OpenFile”, “SaveFile”, and “CloseFile”, testing for those button clicks this way:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "OpenFile"
...
        Case "SaveFile"
...
        Case "CloseFile"
...
    End Select
End Sub

If one of those buttons were clicked, you simply call the associated menu item’s Click event handler function directly:

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "OpenFile"
            mnuFileOpen_Click
        Case "SaveFile"
            mnuFileSave_Click
        Case "CloseFile"
            mnuFileClose_Click
    End Select
End Sub

And that’s all it takes. Now we’ve connected toolbar buttons to menu items.

Adding Separators To A Toolbar

The Aesthetic Design Department is calling again. Can’t you group the buttons in your toolbar into logical groups as you do with items in a menu?

You can, and just in the same way—by using separators. In menus, separators appear as solid lines, but in toolbars, separators just appear as blank spaces, setting groups of buttons apart.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.