Export (0) Print
Expand All

ToolBarButton Class

Represents a Windows toolbar button.

For a list of all members of this type, see ToolBarButton Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.ToolBarButton

[Visual Basic]
Public Class ToolBarButton
   Inherits Component
[C#]
public class ToolBarButton : Component
[C++]
public __gc class ToolBarButton : public Component
[JScript]
public class ToolBarButton extends Component

Thread Safety

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

Remarks

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 buttonto 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 give 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.

Example

[Visual Basic, C#, C++] The following 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 assumes that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have all been created.

[Visual Basic] 
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.
    toolBar1.Buttons.Add(toolBarButton1)
    toolBar1.Buttons.Add(toolBarButton2)
    toolBar1.Buttons.Add(toolBarButton3)
    
    ' Add the event-handler delegate.
    AddHandler toolBar1.ButtonClick, AddressOf Me.toolBar1_ButtonClick
    
    ' Add the ToolBar to the Form.
    Controls.Add(toolBar1)
End Sub    

Protected Sub toolBar1_ButtonClick(sender As Object, _
    e As ToolBarButtonClickEventArgs)
    
    ' Evaluate the Button property to determine which button was clicked.
    Select Case toolBar1.Buttons.IndexOf(e.Button)
        Case 0
            openFileDialog1.ShowDialog()
            ' Insert code to open the file.
        Case 1
            saveFileDialog1.ShowDialog()
            ' Insert code to save the file.
        Case 2
            printDialog1.ShowDialog()
            ' Insert code to print the file.
    End Select        
End Sub


[C#] 
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.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
    
    // Add the event-handler delegate.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
       this.toolBar1_ButtonClick);
    
    // Add the ToolBar to the Form.
    Controls.Add(toolBar1);
 }
 
 protected void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
 {
   // Evaluate the Button property to determine which button was clicked.
   switch(toolBar1.Buttons.IndexOf(e.Button))
   {
      case 0:
         openFileDialog1.ShowDialog();
         // Insert code to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert code to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert code to print the file.    
         break; 
    }
 }


[C++] 
public:
    void InitializeMyToolBar() {
        // Create and initialize the ToolBar and ToolBarButton controls.
        toolBar1 = new ToolBar();
        ToolBarButton __gc *toolBarButton1 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton2 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton3 = new ToolBarButton();

        // Set the Text properties of the ToolBarButton controls.
        toolBarButton1->Text = S"Open";
        toolBarButton2->Text = S"Save";
        toolBarButton3->Text = S"Print";

        // Add the ToolBarButton controls to the ToolBar.
        toolBar1->Buttons->Add(toolBarButton1);
        toolBar1->Buttons->Add(toolBarButton2);
        toolBar1->Buttons->Add(toolBarButton3);

        // Add the event-handler delegate.
        toolBar1->ButtonClick += new ToolBarButtonClickEventHandler(this, &Form1::toolBar1_ButtonClick);

        // Add the ToolBar to the Form.
        Controls->Add(toolBar1);
    };

protected:
    void toolBar1_ButtonClick (Object *sender, ToolBarButtonClickEventArgs *e) {
        // Evaluate the Button property to determine which button was clicked.
        switch(toolBar1->Buttons->IndexOf(e->Button)) {
        case 0:
            openFileDialog1->ShowDialog();
            // Insert code to open the file.
            break; 
        case 1:
            saveFileDialog1->ShowDialog();
            // Insert code to save the file.
            break; 
        case 2:
            printDialog1->ShowDialog();
            // Insert code to print the file.    
            break; 
        };
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

ToolBarButton Members | System.Windows.Forms Namespace | ToolBar | MenuItem | ImageList | ToolTip

Show:
© 2014 Microsoft