Export (0) Print
Expand All

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)

public class ToolBar : Control
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ToolBar extends Control
public class ToolBar extends Control
Not applicable.

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 2003 for Pocket PC, Windows Mobile 2003 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 void InitializeMyToolBar()
    // Create and initialize the ToolBar and ToolBarButton controls.
    toolBar1 = new ToolBar();
    ToolBarButton toolBarButton1 = new ToolBarButton();
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = 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.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
    // Add the ToolBar to the Form.
 private void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
   // Evaluate the Button property to determine which button was clicked.
      case 0:
         // Insert code to open the file.
      case 1:
         // Insert code to save the file.
      case 2:
         // Insert code to print the file.    

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

© 2014 Microsoft