How To Add a Toolbar to a Visual Basic Project

Last reviewed: June 28, 1996
Article ID: Q153054
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic for Windows, 32-bit only, version 4.0

SUMMARY

A feature of the Windows 95 user interface is the toolbar. A toolbar is a collection of buttons that activate some of the most commonly used features of a program. Visual Basic allows you to add a toolbar to your program through the Toolbar custom control. This article explains the toolbar custom control and provides a sample program that uses this Toolbar control.

MORE INFORMATION

The Toolbar control is a 32-bit custom control for use only in a 32-bit operating systems, such as Windows 95 and Windows NT version 3.51. The control acts as a container for button objects. Buttons can be added to the toolbar using the Add method, as demonstrated in the sample application or by setting the [Custom] property of the Toolbar control. In addition to performing a function when clicked, buttons can also contain descriptions and ToolTip text. A ToolTip is a one or two word description of the button s function and is displayed when the cursor is passed over the button.

To use the Toolbar control, you must add the COMCTL32.OCX file to your project. When you ship your program, you must install COMCTL32.OCX into your user's Windows SYSTEM directory.

SAMPLE PROGRAM WITH A TOOLBAR

The sample program is a simple word processor that uses a toolbar to perform some functions, such as opening a file, saving a file, and formatting the document. The program contains the Rich Text Box control where the user types in a document, a toolbar control containing buttons to perform a function, an Image List control to store the bitmaps appearing on the buttons, and a Common Dialog control to perform the file open and save functions.

In this example, the buttons are added programmatically in the Load event of the Form. The properties of the buttons are also set during the load event. The button's function are set in the ButtonClick event of the Toolbar control.

The toolbar has the added feature of wrapping the buttons when the form is resized and customizing the positions of all the buttons. Wrapping the buttons is enabled by setting the Wrappable property of the Toolbar to True.

A button is used to customize the position of the different buttons. Clicking this button displays a dialog box that allows you to add a separator bar, remove buttons, and rearrange the button position. Before the dialog box displays, the current button positions are stored in the registry. When you click the Restore Toolbar button, the information stored in the registry is used to reset the positions of the buttons.

Steps to Create the Sample Program

  1. Start the 32-bit version of Visual Basic 4.0 or if it is already running, click New Project on the File menu.

  2. Add a Toolbar, Rich Text Box, Image List, and a Common Dialog control to the Form1 form.

  3. Copy the following code to the Form1 code window.

          Option Explicit
    

          'SaveToolbar method constants.
          Const SaveToolbarKey = 1
          Const SaveToolbarSubKey = "MyToolbar"
          Const SaveToolbarVal = "True"
    

          Private Sub Form_Load()
    
             'Create object variable for the ImageList.
             Dim imgX As ListImage
    
             'Load pictures for the toolbar buttons into the ImageList control.
             Set imgX = ImageList1.ListImages. _
                Add(, "open", LoadPicture("bitmaps\tlbr_w95\open.bmp"))
            Set imgX = ImageList1.ListImages. _
                Add(, "save", LoadPicture("bitmaps\tlbr_w95\save.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "left", LoadPicture("bitmaps\tlbr_w95\lft.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "right", LoadPicture("bitmaps\tlbr_w95\rt.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "center", LoadPicture("bitmaps\tlbr_w95\cnt.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "justify", LoadPicture("bitmaps\tlbr_w95\jst.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "bold", LoadPicture("bitmaps\tlbr_w95\bld.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "italic", LoadPicture("bitmaps\tlbr_w95\Itl.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "customize", LoadPicture("bitmaps\tlbr_w95\prop.bmp"))
    
             Set imgX = ImageList1.ListImages. _
                Add(, "restore", LoadPicture("bitmaps\tlbr_w95\redo.bmp"))
    
             Toolbar1.ImageList = ImageList1
    
             'Create object variable for the Toolbar.
             Dim btnX As Button
    
             'Add button objects to Buttons collection using the Add method.
             'After creating the button, set the Description and ToolTipText
             'properties.
    
             'Create Open Button
             Set btnX = Toolbar1.Buttons.Add(, "open", , tbrDefault, "open")
                btnX.ToolTipText = "Open File"
                btnX.Description = btnX.ToolTipText
    
             'Create Save Button
             Set btnX = Toolbar1.Buttons.Add(, "save", , tbrDefault, "save")
                btnX.ToolTipText = "Save File"
                btnX.Description = btnX.ToolTipText
    
             'Add a Separator
             Set btnX = Toolbar1.Buttons.Add(, , , tbrSeparator)
    
             'Create Paragraph Align Left button
             Set btnX = Toolbar1.Buttons.Add(, "left", , tbrButtonGroup, _
                                               "left")
                btnX.ToolTipText = "Align Left"
                btnX.Description = btnX.ToolTipText
    
             'Create Paragraph Align Center button
             Set btnX = Toolbar1.Buttons.Add(, "center", , _
                                             tbrButtonGroup, "center")
                btnX.ToolTipText = "Center"
                btnX.Description = btnX.ToolTipText
    
             'Create Paragraph Align Right button
             Set btnX = Toolbar1.Buttons.Add(, "right", , tbrButtonGroup, _
                                               "right")
                btnX.ToolTipText = "Align Right"
                btnX.Description = btnX.ToolTipText
    
             'Add a Separator
             Set btnX = Toolbar1.Buttons.Add(, , , tbrSeparator)
    
             'Create a Bold Button
             Set btnX = Toolbar1.Buttons.Add(, "bold", , tbrCheck, "bold")
                btnX.ToolTipText = "Bold"
                btnX.Description = btnX.ToolTipText
    
             'Italic Button
             Set btnX = Toolbar1.Buttons.Add(, "italic", , tbrCheck, _
                                               "italic")
                btnX.ToolTipText = "Italic"
                btnX.Description = btnX.ToolTipText
    
             'Add a Separator
             Set btnX = Toolbar1.Buttons.Add(, , , tbrSeparator)
    
             'Create a Customize Toolbar button
             Set btnX = Toolbar1.Buttons.Add(, "customize", , tbrCheck, _
                                             "customize")
                btnX.ToolTipText = "Customize Toolbar"
                btnX.Description = btnX.ToolTipText
    
             'Create a Restore Toolbar button
             Set btnX = Toolbar1.Buttons.Add(, "restore", , tbrCheck, _
                                             "restore")
                btnX.ToolTipText = "Restore Toolbar"
                btnX.Description = btnX.ToolTipText
    
             With Toolbar1
                .Wrappable = True ' Buttons can wrap.
    
                'Prevent customization except by clicking Customize button.
                .AllowCustomize = False
             End With
    
             'Configure commondialog1 for opening and saving files.
             With CommonDialog1
                .DefaultExt = ".rtf"
                .Filter = "RTF file (*.RTF)|*.RTF"
             End With
    
             'Set margin of the RichTextBox to the width of the control.
             richtextbox1.RightMargin = richtextbox1.Width
    
          End Sub
    
          Private Sub richtextbox1_SelChange()
          'When the insertion point changes, set the Toolbar buttons to
          'reflect the attributes of the text where the cursor is located.
          'Use the Select Case statement. The SelAlignment property returns
          'either 0, 1, 2, or Null.
    
             Select Case richtextbox1.SelAlignment
                Case Is = rtfLeft ' 0
                   Toolbar1.Buttons("left").Value = tbrPressed
    
                Case Is = rtfRight '1
                   Toolbar1.Buttons("right").Value = tbrPressed
    
                Case Is = rtfCenter '2
                   Toolbar1.Buttons("center").Value = tbrPressed
    
                Case Else ' Null -- No buttons are shown in the up position.
                   Toolbar1.Buttons("left").Value = tbrUnpressed
                   Toolbar1.Buttons("right").Value = tbrUnpressed
                   Toolbar1.Buttons("center").Value = tbrUnpressed
    
             End Select
    
             'SelBold returns 0, -1, or Null.  If it's Null then set the
             'MixedState property to True.
             Select Case richtextbox1.SelBold
                Case 0 ' Not bold.
                   Toolbar1.Buttons("bold").Value = tbrUnpressed
    
                Case -1 ' Bold.
                   Toolbar1.Buttons("bold").Value = tbrPressed
    
                Case Else ' Mixed state.
                   Toolbar1.Buttons("bold").MixedState = True
    
             End Select
    
             'SelItalic returns 0, -1, or Null.  If it's Null then set the
             'MixedState property to True.
             Select Case richtextbox1.SelItalic
                Case 0 ' Not italic.
                   Toolbar1.Buttons("italic").Value = tbrUnpressed
    
                Case -1 ' Italic.
                   Toolbar1.Buttons("italic").Value = tbrPressed
    
                Case Else ' Mixed State.
                   Toolbar1.Buttons("italic").MixedState = True
    
             End Select
    
          End Sub
    
          Private Sub toolbar1_ButtonClick(ByVal Button As Button)
          'Use the Key property with the SelectCase statement to specify
          'an action.
             Select Case Button.Key
                Case Is = "open"           ' Open file.
                   Dim strOpen As String   ' String variable for file name.
                   CommonDialog1.ShowOpen  ' Show Open File dialog box.
                   strOpen = CommonDialog1.filename ' Set variable to filename.
                   richtextbox1.LoadFile strOpen, 0 ' Use LoadFile method.
    
                Case Is = "save"              ' Save file.
                   Dim strNewFile As String   ' String variable for new
                                              ' file name.
                   CommonDialog1.ShowSave     ' Show Save dialog box.
                   strNewFile = CommonDialog1.filename ' Set variable to
                                                       'file name.
                   richtextbox1.SaveFile strNewFile, 0 ' Use SaveFile method.
    
                Case Is = "left"
                   richtextbox1.SelAlignment = rtfLeft
    
                Case Is = "center"
                   richtextbox1.SelAlignment = rtfCenter
    
                Case Is = "right"
                   richtextbox1.SelAlignment = rtfRight
    
                Case Is = "bold"
                   'Test to see if the MixedState property is True. If so,
                   'then set it to false before doing anything else.
                   If Button.MixedState = True Then
                      Button.MixedState = False
                   End If
    
                   'Toggle the SelBold property.
                   richtextbox1.SelBold = Abs(richtextbox1.SelBold) - 1
    
                Case Is = "italic"
                   'Test to see if the MixedState property is True. If so,
                   'then set it to false before doing anything else.
                   If Button.MixedState = True Then
                      Button.MixedState = False
                   End If
    
                   'Toggle the SelItalic property.
                   richtextbox1.SelItalic = Abs(richtextbox1.SelItalic) - 1
    
                Case Is = "customize"
                   'Save the state of Toolbar1 in the registry before
                   'allowing further customization.
                   With Toolbar1
                      .SaveToolbar SaveToolbarKey, SaveToolbarSubKey, _
                                   SaveToolbarVal
                      .AllowCustomize = True 'AllowCustomize must be True to
                                             'change toolbar.
                      .Customize     'Customize method invokes Customize
                                     'Dialog box.
                      .AllowCustomize = False 'After customization, set this
                                              'to False.
                   End With
    
                Case Is = "restore"
                   'Restore state of Toolbar1 using the information stored in
                   'the registry.
                   Toolbar1.RestoreToolbar SaveToolbarKey, _
                                           SaveToolbarSubKey, SaveToolbarVal
    
             End Select
    
          End Sub
    
    

  4. On the Run menu, click Start or press the F5 key to start the program. Pass the cursor over each button to display the Tooltip describing the button's function. Click the button to perform the function.

REFERENCES

   Microsoft Visual Basic: Professional Features: Custom Control
   Reference." See the Toolbar control, pages 437-451.


Additional reference words: 4.00 vb4win vb432
KBCategory: kbprg kbhowto
KBSubcategory: PrgCtrlsCus



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 28, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.