Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the toolbar control
DescriptionThis example shows how to use the Toolbar control in Visual Basic 6. It explains how to use a ListView control to hold toolbar button images, how to associate those images with the buttons, and how to respond to button clicks.
Keywordstoolbar, form
CategoriesControls
 
Creating a Toolbar is not trivial. There are several steps to follow and some need to be performed in the right order. This HowTo's real usefulness is in its listing of the steps:

  1. Press Ctrl-T to open the Tools dialog. Select Microsoft Windows Common Controls.
  2. Add an ImageList control to the form.
    1. Left-click on the ImageList and select the Properties command.
    2. Click on the Images tab.
    3. Click the Insert Picture button and find your button bitmaps. You can probably find the standard Windows 95 bitmaps in the Common/Graphics section of your computer's programming area. On my computer this is:
          C:\Program Files\Microsoft Visual Studio\Common\Graphics\Bitmaps\TlBr_W95
    4. When you have added all your button images, click the Ok button.
  3. Add a Toolbar control to your form.
    1. Left-click on the Toolbar and select the Properties command.
    2. On the General tab, find the ImageList field (3rd from the top in my version). Select the ImageList control from step 2.
    3. Click the Buttons tab.
    4. Click Insert Button to add a button to the Toolbar. Fill in appropriate fields:
      1. Caption appears on the button. For a normal button, leave this blank.
      2. Key is passed to the Toolbar's event handler. This is one way you can tell which button was clicked. Enter a short key word here.
      3. ToolTipText is displayed when the user lets the mouse rest over the button. Enter something short but helpful here. Remember that the user sees only a small picture if Caption is blank, so even very short text is useful here. For example, "Cut" is a good tip for the cut button.
      4. Image is the index of the button's picture in the ImageList control. 0 means no image. 1 is the first image in the ImageList.
      5. Experiment with the other button properties to see how they work.
    5. When you have added all the buttons, click Ok.
  4. Double click on the Toolbar to open the code editor in its ButtonClick event handler. This routine takes as a parameter a Button object representing the button pressed. Look at Button.Key to see which button was pressed.

Note that if you modify the ImageList after you have attached it to the Toolbar, you may mess up the Toolbar. For this reason, you should try to put every picture you will need in the ImageList first. Then initialize the Toolbar.

When the user clicks a button, the Toolbar control's ButtonClick event fires. You can use the buton's Key property to tell which button was clicked.

 
Private Sub Toolbar1_ButtonClick(ByVal Button As _
    MSComctlLib.Button)
    Select Case Button.Key
        Case "Cut"
            MsgBox "Cut"
        Case "Copy"
            MsgBox "Copy"
        Case "Center"
            MsgBox "Center"
        Case "Snapshot"
            MsgBox "Snapshot"
    End Select
End Sub
 
Some additional tips from Gary German:

  • Toolbar graphics can be assigned from external files at runtime. But, the graphics on each toolbar must all be the same size, otherwise they'll look bad. Specifically, the first button to which a graphic is assigned "wins", and all following graphics will be stretched, if necessary, to fit in that same size.

  • Max number of buttons on a toolbar is 255.

  • If you need to create some buttons with graphics, and others with text only, don't use the button's .Caption property - it will cause all buttons to grow vertically because the text is placed below the missing graphic. Instead, use a routine to turn the text you want to display into a graphic, and assign this "graphic" to the image list, and then the toolbar button.
 
WJKalso points out that the Toolbar Wizard can make building toolbars a lot simpler. In VB 6, open the Add-Ins menu and select Add-In Manager. Double-click the Application Wizard to start it and click OK.

Now the Add-Ins menu contains the Toolbar Wizard. The Wizard lets you build a toolbar quickly and easily, particularly if you want to use the standard toolbar buttons (Copy, Cut, Paste, Justify, Open, New, and so forth).

If you place a toolbar on your form, the Wizard also automatically appears to help you build it.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated