Export (0) Print
Expand All

ToolBarButton Class

Represents a Windows toolbar button. Although ToolStripButton replaces and extends the ToolBarButton control of previous versions, ToolBarButton 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 ToolBarButton : Component

The ToolBarButton type exposes the following members.

Public methodToolBarButton()Initializes a new instance of the ToolBarButton class.
Public methodToolBarButton(String)Initializes a new instance of the ToolBarButton class and displays the assigned text on the button.

Public propertyDropDownMenuGets or sets the menu to be displayed in the drop-down toolbar button.
Public propertyEnabledGets or sets a value indicating whether the button is enabled.
Public propertyImageIndexGets or sets the index value of the image assigned to the button.
Public propertyImageKeyGets or sets the name of the image assigned to the button.
Public propertyNameThe name of the button.
Public propertyParentGets the toolbar control that the toolbar button is assigned to.
Public propertyPartialPushGets or sets a value indicating whether a toggle-style toolbar button is partially pushed.
Public propertyPushedGets or sets a value indicating whether a toggle-style toolbar button is currently in the pushed state.
Public propertyRectangleGets the bounding rectangle for a toolbar button.
Public propertyStyleGets or sets the style of the toolbar button.
Public propertyTagGets or sets the object that contains data about the toolbar button.
Public propertyTextGets or sets the text displayed on the toolbar button.
Public propertyToolTipTextGets or sets the text that appears as a ToolTip for the button.
Public propertyVisibleGets or sets a value indicating whether the toolbar button is visible.

Protected methodDispose(Boolean)Infrastructure. Releases the unmanaged resources used by the ToolBarButton and optionally releases the managed resources. (Overrides Component.Dispose(Boolean).)
Public methodToStringInfrastructure. Returns a string that represents the ToolBarButton control. (Overrides Component.ToString().)


ToolBarButton controls are parented by ToolBar controls. Common properties to set once the toolbar button has been created are Text and ImageIndex. Set the Text property of the button to display text beneath or to the right of the image. To assign images to the buttons by creating an ImageList, assigning it to the ImageList property of the toolbar; then assign the image index value to the ImageIndex property of the button.

To change the appearance of the toolbar buttons assigned to the toolbar, set the Appearance property of the parent toolbar control. The ToolBarAppearance.Flat appearance gives the buttons a flat appearance. As the mouse pointer moves over the buttons, their appearance changes to three-dimensional. Button separators appear as lines rather than spaces between the buttons when the buttons have a flat appearance. If the Appearance property is set to ToolBarAppearance.Normal, the buttons appear raised and three-dimensional, and the separators appear as a gap between the buttons.

You can assign a ContextMenu to a button if the Style property is set to ToolBarButtonStyle.DropDown. When the button is clicked, the assigned menu is displayed.

To create a collection of ToolBarButton controls to display on a ToolBar, add the buttons individually by using the Add method of the Buttons property. Alternatively, you can add several toolbar buttons using the AddRange method.

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 all 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.    

.NET Framework


.NET Framework Client Profile

Supported in: 4, 3.5 SP1




Community Additions

© 2014 Microsoft