The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
2 Common Controls Library &nbsp
Toolbars and Rebars: Quick Start 1 - Add a Rebar and a Toolbar to Your App
How to get started with the vbAccelerator CoolMenu, Toolbar and Rebar Control. &nbsp
&nbsp



This sample explains how to create a standard document style toolbar with text captions on the buttons.

  1. Add a Rebar and Toolbar control to your form.
    It doesn't matter where you put them, they will be sized to position when the app runs. If your form is an MDI form, also add a PictureBox and set it's Align property to 1 (Top).

  2. Set up the toolbar
    The following code initialises the toolbar images to use the in-built standard toolbar button Image List, and then creates the toolbar with the CreateToolbar method:

With cToolbar1
.ImageSource = CTBStandardImageSources
.ImageStandardBitmapType = CTBStandardSmallColor
.CreateToolbar 16, , True
End With

  • Set up the buttons
    The list of available in-built images are coded as an enumeration in the toolbar control. Check the ECTBStandardImageIndexConstants enumeration for a list of the available items. The AddButton method allows you to add or insert a toolbar button and takes the following arguments:
    • sTip - the tooltip
    • iImage - the 0 based index of the bitmap image to use
    • vButtonBefore - the key or 0 based index of the button to insert before, or leave missing to add a button to end of the toolbar.
    • sButtonText - the text to appear under the button, or nothing if your button does not have text.
    • eButtonStyle - the style of the button, i.e. standard, autosized, drop-down, check or check group. The ECTBToolButtonSyle contains the allowable styles for this parameter.
    • sKey - A string key which can be used to identify the button later.

  • &nbsp &nbsp With cToolbar1
    &nbsp &nbsp &nbsp &nbsp .AddButton "New", CTBStdFileNew, , , "New", CTBDropDown,"New"
    &nbsp &nbsp &nbsp &nbsp .AddButton "Open", CTBStdFileOpen, , , "Open", ,"Open"
    &nbsp &nbsp &nbsp &nbsp .AddButton "Save", CTBStdFIleSave, , , "Save", ,"Save"
    &nbsp &nbsp &nbsp &nbsp .AddButton "", -1, , , , CTBSeparator
    &nbsp &nbsp &nbsp &nbsp .AddButton "Cut", CTBStdCut, , , "Cut", ,"Cut"
    &nbsp &nbsp &nbsp &nbsp .AddButton "Copy", CTBStdCopy, , , "Copy", ,"Copy"
    &nbsp &nbsp &nbsp &nbsp .AddButton "Paste", CTBStdPaste, , , "Paste", ,"Paste"
    &nbsp &nbsp &nbsp &nbsp .AddButton "", -1, , , , CTBSeparator
    &nbsp &nbsp &nbsp &nbsp .AddButton "CheckBox", CTBStdProperties, , , "Properties", CTBCheck,"Properties"
    &nbsp &nbsp &nbsp &nbsp .AddButton "", -1, , , , CTBSeparator
    &nbsp &nbsp &nbsp &nbsp .AddButton "Print", CTBStdPrint, , , "Print", ,"Print"
    &nbsp &nbsp &nbsp &nbsp .AddButton "", -1, , , , CTBSeparator
    &nbsp &nbsp &nbsp &nbsp .AddButton "Help", CTBStdHelp, , , "Help", ,"Help"
    &nbsp &nbsp &nbsp &nbsp .AddButton "Find", CTBStdFind, , , "Find", ,"Find"
    &nbsp &nbsp End With

  • Set up the rebar and add the toolbar
    Here the rebar does not have a background bitmap. The rebar is created with the CreateRebar method, which takes the hWnd of the rebar's parent as an argument, and then the toolbar is added to it with the AddBandByhWnd method, which takes the following arguments:
    • hWnd - the hWnd of the control to add the band
    • sBandText - the text to show at the start of the band, if any
    • bBreakLine - Set to True to force the band to initially appear on a new line in the Rebar.
    • bFixedSize - Whether the band can be resized or not within the rebar. Note this method won't work unless you have COMCTL32.DLL version 4.71 or higher.
    • vData - A string key which can be used to identify the rebar band later.

  • &nbsp &nbsp With cReBar1
    &nbsp &nbsp &nbsp &nbsp .CreateRebar Me.hWnd
    &nbsp &nbsp &nbsp &nbsp .AddBandByHwnd cToolbar1.hWnd, , , , "Toolbar1"
    &nbsp &nbsp End With
  • Respond to resizing
    Firstly, you should tell the Rebar to resize whenever the form changes size using the RebarSize method:

  • Private Sub Form_Resize()
    &nbsp &nbsp ' Make sure the rebar is the correct width:
    &nbsp &nbsp cReBar1.RebarSize
    &nbsp &nbsp ' Do your form resizing here:
    &nbsp &nbsp ' Note that the Rebar's height can be obtained from the RebarHeight
    &nbsp &nbsp ' property. This is in pixels, so multiply by Screen.TwipsPerPixelY.
    &nbsp &nbsp ' ...
    End Sub

    The rebar can also change height when your form hasn't resized, for example, when a band is moved by the user. When this occurs, the Rebar raises a HeightChanged event. Respond to this and do all the Form_Resize code you would normally perform.

  • For safety, clear up the rebar when the form unloads
    Although the control should theoretically respond correctly during unload, the unload order is sometimes unpredictable in VB. By calling this method you ensure all controls are in the correct place and that the Rebar will terminate.

  • Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    &nbsp &nbsp ' For safety, ensure that the Rebar doesn't have any
    &nbsp &nbsp ' child windows before terminating. This shouldn't be
    &nbsp &nbsp ' necessary:
    &nbsp &nbsp cReBar1.RemoveAllRebarBands
    End Sub
  • Respond to button clicks
    When a button is clicked, the toolbar will raise a ButtonClick event, passing the index of the button as a parameter. You can get the key of the button by querying the toolbar's ButtonKey property.
    If the drop-down portion of a button is clicked, the toolbar will raise a DropDownPress event, also passing the index of the button. To show a drop-down menu, call the toolbar's GetDropDownPosition method to determine the x and y position to show the menu, and use VB's PopupMenu event to show a menu there. And that's it! Now you are ready to continue to stage 2: Quick Start 2: Adding a CoolMenu to your Application.



  • TopBack to top

    BackBack to CoolMenu Toolbar and Rebar Control

    Source Code - What We're About!Back to Source Code

    &nbsp

    AboutContributeSend FeedbackPrivacy

    Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
    Last updated: 30 May 1999