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


That’s all we need—now run the program, as shown in Figure 15.16.


Figure 15.16  Adding a combo box to a toolbar.

When users make a selection with the combo box, we display a message box letting them know what they’ve selected. Our combo box toolbar example is a success.

The code for this example appears in the combotoolbar folder on this book’s accompanying CD-ROM.

Setting Toolbar Button Tool Tips

Giving toolbar buttons tool tips (those small yellow windows that display explanatory text when the mouse cursor rests on the underlying control) is an easy process. All you need to do to give a button a tool tip is to set its ToolTipText property.

To set the ToolTipText property, right-click the toolbar and select the Properties item in the menu that opens. Click the Buttons tab and select the button you want to add the tool tip to. Place the tool tip text in the box labeled ToolTipText, as shown in Figure 15.17. Finally, close the property pages by clicking on OK. Now when you run the program, the button displays a tool tip, as shown in Figure 15.18.


Figure 15.17  Setting a toolbar button’s tool tip text.


Figure 15.18  Toolbar buttons with tool tips.

Letting The User Customize The Toolbar

The Testing Department has sent you a memo. Some users of your new program, SuperDuperTextPro, want the Save button at left in the toolbar, but other users want the Create New Document button there. What can we do?

You can let the user customize the toolbar. Just set the AllowCustomize property to True (the default). When the user double-clicks the toolbar, the Customize Toolbar dialog box appears, as shown in Figure 15.19. Users can customize the toolbar as they like using that dialog box.


Figure 15.19  Using the Customize Toolbar dialog box.


TIP:  If you allow your end user to reconfigure the toolbar control, you can save and restore the toolbar by using the SaveToolbar and RestoreToolbar methods.

Adding Toolbar Buttons At Runtime

How do you add buttons to a toolbar at runtime? It’s possible to add menu items to menus, so it should be possible to add buttons to toolbars.

It is. To add a new button when the user clicks a button, we start by declaring a new Button object:

Private Sub Command1_Click()
    Dim Button1 As Button
...
End Sub

Next, we add a new button to the toolbar’s Buttons collection, which is how it stores its buttons internally. As with all collections, the Buttons collection has an Add method, and we use it here:

Private Sub Command1_Click()
    Dim Button1 As Button
    Set Button1 = Toolbar1.Buttons.Add()
...
End Sub

Now we’re free to set the button’s style. Here, we make it a standard button by setting its Style property to tbrDefault (other options include tbrButtonGroup, tbrSeparator, tbrCheck, tbrPlaceHolder, and tbrDropDown):

Private Sub Command1_Click()
    Dim Button1 As Button
    Set Button1 = Toolbar1.Buttons.Add()
    Button1.Style = tbrDefault
...
End Sub

We can also give the new button a caption:

Private Sub Command1_Click()
    Dim Button1 As Button
    Set Button1 = Toolbar1.Buttons.Add()
    Button1.Style = tbrDefault
    Button1.Caption = "New button"
...
End Sub

Finally, we give the new button a tool tip:

Private Sub Command1_Click()
    Dim Button1 As Button
    Set Button1 = Toolbar1.Buttons.Add()
    Button1.Style = tbrDefault
    Button1.Caption = "New button"
    Button1.ToolTipText = "New button"
End Sub

And that’s it—the new button is active. It’s been added to the Buttons collection of the toolbar control, which means it has its own Index value. That Index value will be passed to the ButtonClick handler, and we can make use of the index this way (you can also set a button’s key text from code by setting its Key property):

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

Adding A Status Bar To A Program

The Testing Department is calling again. Your new SuperDuperDataCrunch program looks good, but what about the status bar? You ask, what status bar? Exactly, they say.

How can you add a status bar to your program? You could design the program with the Visual Basic Application Wizard, which automatically adds a status bar (see “Adding A Toolbar To A Form” earlier in this chapter for more information). However, most programmers will want to add their own status bar to their programs, and you create a status bar by adding a status bar 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 Status Bar Control tool to the Visual Basic toolbox, as shown in Figure 15.2. To place a status bar in your form, just double-click the Status Bar Control.

Now you’ve got a new status bar—but how do you align it at the top of the window and display text in it? See the next couple of topics in this chapter.

Aligning Status Bars In A Form

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

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

Adding Panels To A Status Bar

Now that you’ve added a status bar to your program, it’s time to take the next step: adding panels to the status bar. The text in a status bar is displayed in those panels.

A status bar control has a Panels collection, and you add the panels you want to that collection. To do that at design time, follow these steps:

1.  Right-click the status bar, and select the Properties item in the menu that opens.
2.  Click the Panels tab in the property pages, as shown in Figure 15.20.


Figure 15.20  Adding a panel to a status bar.

3.  Click the Insert Panel button as many times as you want panels in your status bar.
4.  Close the property pages by clicking on OK.


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.