This documentation is archived and is not being maintained.

ToolBar Class

Represents a Windows toolbar. Although ToolStrip replaces and adds functionality to the ToolBar control of previous versions, ToolBar is retained for both backward compatibility and future use if you choose.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ToolBar _
	Inherits Control
Dim instance As ToolBar

ToolBar controls are used to display ToolBarButton controls that can appear as a standard button, a toggle-style button, or a drop-down style button. You can assign images to the buttons by creating an ImageList, assigning it to the ImageList property of the toolbar, and assigning the image index value to the ImageIndex property each ToolBarButton. You can then assign text to be displayed underneath or to the right of the image by setting the Text property of the ToolBarButton.

Set the Appearance property of the toolbar to Flat to give the toolbar and its buttons a flat appearance. As the mouse pointer moves over the buttons, their appearance changes to three-dimensional. Toolbar buttons can be divided into logical groups by using separators. A separator is a toolbar button with the Style property set to ToolBarButtonStyle.Separator. Button separators appear as lines rather than spaces between the buttons when the toolbar has a flat appearance. If the Appearance property is set to Normal, the toolbar buttons appear raised and three-dimensional.

If you specify a value for the ButtonSize property, all buttons in the tool bar are restricted to the specified size. Otherwise, the buttons adjust their size depending on their content, and the ButtonSize property returns the initial size of the largest button.

To create a collection of ToolBarButton controls to display on the ToolBar, add the buttons individually by using the Add or Insert methods of the Buttons property.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: A form supports only one ToolBar, attempts to add an additional ToolBar throws a NotSupportedException. Adding a ToolBar to any control besides a form is not supported, such as to a Panel.

The following code example creates a ToolBar and three ToolBarButton controls. The toolbar buttons are assigned to the button collection, the collection is assigned to the toolbar, and the toolbar is added to the form. On the ButtonClick event of the toolbar, the Button property of the ToolBarButtonClickEventArgs is evaluated, and the appropriate dialog box opened. This code requires that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have been created.

Public Sub InitializeMyToolBar()
    ' Create and initialize the ToolBar and ToolBarButton controls. 
    Dim toolBar1 As New ToolBar()
    Dim toolBarButton1 As New ToolBarButton()
    Dim toolBarButton2 As New ToolBarButton()
    Dim toolBarButton3 As New ToolBarButton()

    ' Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open"
    toolBarButton2.Text = "Save"
    toolBarButton3.Text = "Print" 

    ' Add the ToolBarButton controls to the ToolBar.

    ' Add the event-handler delegate. 
    AddHandler toolBar1.ButtonClick, AddressOf Me.toolBar1_ButtonClick

    ' Add the ToolBar to the Form.
End Sub     

Private Sub toolBar1_ButtonClick(ByVal sender As Object, _
ByVal e As ToolBarButtonClickEventArgs)

    ' Evaluate the Button property to determine which button was clicked. 
    Select Case toolBar1.Buttons.IndexOf(e.Button)
        Case 0
            ' Insert code to open the file. 
        Case 1
            ' Insert code to save the file. 
        Case 2
            ' Insert code to print the file. 
    End Select 
End Sub

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0