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
 
 
 
 
 
 
TitleLet the user decide which ToolBar buttons are visible in VB .NET
DescriptionThis example shows how to let the user decide which ToolBar buttons are visible in VB .NET. It uses a dialog that lets the user select and deselect the buttons.
KeywordsToolBar, customize, customization, configure, VB.NET
CategoriesVB.NET, Controls, Software Engineering
 
In VB 6, the user can double-click a Toolbar control to customize it. This feature is missing in VB .NET.

This simple example lets the user determine which ToolBar buttons are visible. You could modify it to allow the user perform other ToolBar operations such as reordering the buttons.

When the user double-clicks on the form's ToolBar, the program makes a new dlgCustomizeToolBar dialog and calls its CustomizeToolbar method.

 
' Let the user customize the ToolBar.
Private Sub tbrActions_DoubleClick(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles _
    tbrActions.DoubleClick
    Dim dlg As New dlgCustomizeToolBar
    dlg.CustomizeToolbar(tbrActions)
End Sub
 
The dlgCustomizeToolBar dialog lets the user select the ToolBar buttons that should be visible. The main program should call its CustomizeToolbar method rather than calling Show or ShowDialog. To prevent the program from calling either of those routines, the dialog shadows the Form class's implmentations of Show and ShowDialog and throws an exception.
 
' Don't let the program show the dialog normally.
Public Shadows Sub Show()
    Throw New NotSupportedException("Show is not supported. " & _
        "Use the CustomizeToolbar method instead.")
End Sub
Public Shadows Sub ShowDialog()
    Throw New NotSupportedException("ShowDialog is not " & _
        "supported. Use the CustomizeToolbar method " & _
        "instead.")
End Sub
 
The CustomizeToolbar subroutine adds buttons to the dialog's ToolBar that match those in the client ToolBar. It sets each new button's Pushed property to True if the corresponding client button is visible.

The dialog then calls its base class's ShowDialog method to display the dialog modally. Notice how it calls the base class's vesion of ShowDialog instead of calling its own version, which would throw an exception.

 
Private m_ClientToolBar As ToolBar = Nothing

' Display the dialog.
Public Sub CustomizeToolbar(ByVal client_toolbar As ToolBar)
    m_ClientToolBar = client_toolbar

    ' Copy the client ToolBar's buttons.
    tbrButtons.ImageList = client_toolbar.ImageList
    For Each btn As ToolBarButton In client_toolbar.Buttons
        ' Copy this the button.
        Dim new_btn As New ToolBarButton
        new_btn.ImageIndex = btn.ImageIndex
        new_btn.Text = btn.Text
        new_btn.Style = btn.Style
        new_btn.Pushed = btn.Visible
        new_btn.ToolTipText = btn.ToolTipText
        tbrButtons.Buttons.Add(new_btn)
    Next btn

    ' Display the dialog modally.
    MyBase.ShowDialog()
End Sub
 
When the user clicks on of the dialog's ToolBar buttons, the code toggles that button's Pushed state.

If the user clicks the Select All or Deselect All buttons, the dialog sets its ToolBar buttons' Pushed properties accordingly.

If the user clicks the dialog's OK button, the code sets the Visible property for the client ToolBar's buttons and unloads the dialog by setting its DialogResult property to OK.

 
' Toggle the button.
Private Sub tbrSelected_ButtonClick(ByVal sender As _
    System.Object, ByVal e As _
    System.Windows.Forms.ToolBarButtonClickEventArgs) _
    Handles tbrButtons.ButtonClick
    e.Button.Pushed = Not e.Button.Pushed
End Sub

' Select all of the buttons.
Private Sub btnSelectAll_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnSelectAll.Click
    ' Skip separators.
    For Each btn As ToolBarButton In tbrButtons.Buttons
        If btn.Style <> ToolBarButtonStyle.Separator Then
            btn.Pushed = True
        End If
    Next btn
End Sub

' Deselect all of the buttons.
Private Sub btnDeselectAll_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnDeselectAll.Click
    ' Skip separators.
    For Each btn As ToolBarButton In tbrButtons.Buttons
        If btn.Style <> ToolBarButtonStyle.Separator Then
            btn.Pushed = False
        End If
    Next btn
End Sub

' Update the ToolBar.
Private Sub btnOk_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOk.Click
    ' Examine all buttons.
    For i As Integer = 0 To m_ClientToolBar.Buttons.Count - _
        1
        m_ClientToolBar.Buttons(i).Visible = _
            tbrButtons.Buttons(i).Pushed
    Next i

    Me.DialogResult = DialogResult.OK
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated