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
 
 
 
 
 
 
TitleAdd hidden buttons to a toolbar
DescriptionThis example shows how to add hidden buttons to a toolbar in Visual Basic 6. The program saves the Toolbar's state, adds the buttons, and restores the state.
Keywordstoolbar, SaveToolbar, RestoreToolbar, configuration, hidden buttons
CategoriesControls, Software Engineering
 
If you set a Toolbar control's AllowCustomize property to True, then the user can double-click on it to add, remove, and rearrange the Toolbar's buttons.

You might want to have buttons available through configuration but not initially visible. The user can then select those buttons if desired.

When the program's form is intialized, the code uses the RegOpenKey API function to see if the Toolbar's base state has been saved before. If the state has not been saved yet, the program calls the control's SaveToolbar method to save the state. This state includs only the buttons that were added to the Toolbar at design time.

Next the program adds other buttons to the Toolbar. When it has finished, the code calls the Toolbar's RestoreToolbar method to restore its saved state. This hides the newly added buttons but leaves them available for user configuration.

 
Private Sub Form_Initialize()
Dim hKey As Long

    ' Display the form.
    Me.Show
    DoEvents

    ' See if we have saved the basic state before.
    If RegOpenKey(HKEY_CURRENT_USER, _
        "Software\howto_hidden_toolbar_buttons\Toolbar", _
        hKey) <> 0 Then
        ' We have not saved the state before. Do so now.
        tbrActions.SaveToolbar _
            "Software\howto_hidden_toolbar_buttons", _
            "Toolbar", "Settings"
    End If

    ' Add buttons that should not be displayed initially.
    Dim new_button As Button
    Set new_button = tbrActions.Buttons.Add(Key:="Print", _
        Image:="Print")
    new_button.Tag = new_button.Key
    new_button.Description = new_button.Key

    Set new_button = tbrActions.Buttons.Add(Key:="New", _
        Image:="New")
    new_button.Tag = new_button.Key
    new_button.Description = new_button.Key

    Set new_button = tbrActions.Buttons.Add(Key:="Open", _
        Image:="Open")
    new_button.Tag = new_button.Key
    new_button.Description = new_button.Key
    DoEvents

    ' Restore the saved Toolbar state.
    tbrActions.RestoreToolbar _
        "Software\howto_hidden_toolbar_buttons", _
        "Toolbar", "Settings"
    DoEvents
End Sub
 
Note that this code must execute in the form's Initialize event. It doesn't work in the Load event.

See also Save and restore toolbar configuration when a program starts and stops.

The Microsoft documentation indicates that the added and removed buttons should appear in the configuration dialog. On my system at least, they do not appear, although the user can get them back by clicking the dialog's Reset button.

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