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


Adding Toolbars To Forms

For some reason, adding toolbars to forms isn’t covered in a lot of Visual Basic books. However, users have come to expect toolbars in more complex programs, and we’ll see how to add them here. Toolbars provide buttons that correspond to menu items and give the user an easy way to select the commands those items correspond to.

Adding A Toolbar With The Application Wizard

The easiest way to design a toolbar and add it to a program is with the Application Wizard. When you create a new application using the Application Wizard, it lets you design the toolbar, as shown in Figure 4.5.


Figure 4.5  Designing a toolbar with the Application Wizard.

This is a great way to put a toolbar in a program, because the support is already there for all these buttons by default. When you create the program, here’s how it handles the buttons in the toolbar, with a Select Case statement that looks at the button’s Key value:

Private Sub tbToolBar_ButtonClick(ByVal Button As ComctlLib.Button)
    On Error Resume Next
    Select Case Button.Key
        Case "New"
            LoadNewDoc
        Case "Open"
            mnuFileOpen_Click
        Case "Save"
            mnuFileSave_Click
        Case "Print"
            mnuFilePrint_Click
        Case "Copy"
            mnuEditCopy_Click
        Case "Cut"
            mnuEditCut_Click
        Case "Paste"
            mnuEditPaste_Click
        Case "Bold"
            ActiveForm.rtfText.SelBold = Not ActiveForm.rtfText.SelBold
            Button.Value = IIf(ActiveForm.rtfText.SelBold, tbrPressed,_
                tbrUnpressed)
        Case "Italic"
            ActiveForm.rtfText.SelItalic = Not ActiveForm.rtfText._
                SelItalic
            Button.Value = IIf(ActiveForm.rtfText.SelItalic, tbrPressed,_
                tbrUnpressed)
        Case "Underline"
            ActiveForm.rtfText.SelUnderline = Not _
                ActiveForm.rtfText.SelUnderline
            Button.Value = IIf(ActiveForm.rtfText.SelUnderline,_
                tbrPressed,tbrUnpressed)
        Case "Align Left"
            ActiveForm.rtfText.SelAlignment = rtfLeft
        Case "Align Right"
            ActiveForm.rtfText.SelAlignment = rtfRight
        Case "Center"
            ActiveForm.rtfText.SelAlignment = rtfCenter
    End Select
End Sub

Adding A Toolbar To A Program Yourself

You can also add toolbars to already-existing programs; just follow these steps:

1.  Use the Project[vbar]Components item to open the Components box, and select the Controls tab.
2.  Click the Microsoft Windows Common Controls box, and click on OK to close the Components box.
3.  Double-click the New Toolbar tool in the toolbox to add a new toolbar to your form now.
4.  Right-click the toolbar now, and select the Properties item in the pop-up menu that appears, opening the button’s property page, as shown in Figure 4.6.


Figure 4.6  Setting a toolbar button’s properties.

5.  Click the Buttons tab in the property page now, and click Insert Button to insert a new button into the toolbar.
6.  Give the new button the caption you want, and set its Key property to a string of text you want to refer to the button with in code (in Figure 4.6, we set the new button’s Key property to “First”).
7.  Add other buttons in the same way and close the property page.
8.  Double-click a button in the toolbar now to open the code window, displaying Toolbar1_ButtonClick():
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
...
End Sub
9.  Add the code you want to Toolbar1_ButtonClick(). You do this with a Select Case statement, selecting on the buttons’ Key property:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
    Select Case Button.Key
        Case "First"
            MsgBox "You clicked the first button."
        Case "Second"
            MsgBox "You clicked the second button."
        Case "Third"
            MsgBox "You clicked the third button."
     End Select

End Sub

And that’s it—now we’ve added a toolbar to a program; when the user clicks a key in the toolbar, our program will handle it. The result appears in Figure 4.7.


Figure 4.7  A form with a toolbar.


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.