Muokkaa

Jaa


ToolBarButton Class

Definition

Represents a Windows toolbar button.

This class is not available in .NET Core 3.1 and later versions. Use ToolStripButton instead, which replaces and extends the ToolBarButton control.

public ref class ToolBarButton : System::ComponentModel::Component
public class ToolBarButton : System.ComponentModel.Component
type ToolBarButton = class
    inherit Component
Public Class ToolBarButton
Inherits Component
Inheritance

Examples

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 = gcnew ToolBar;
      ToolBarButton^ toolBarButton1 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton2 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton3 = gcnew 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 += gcnew ToolBarButtonClickEventHandler(
         this, &Form1::toolBar1_ButtonClick );
      
      // Add the ToolBar to the Form.
      Controls->Add( toolBar1 );
   }

private:
   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;
      }
   }
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);
 }
 
 private 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; 
    }
 }
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    

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

Remarks

This class is not available in .NET Core 3.1 and later versions. Use ToolStripButton instead.

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.

Constructors

ToolBarButton()

Initializes a new instance of the ToolBarButton class.

ToolBarButton(String)

Initializes a new instance of the ToolBarButton class and displays the assigned text on the button.

Properties

CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
DropDownMenu

Gets or sets the menu to be displayed in the drop-down toolbar button.

Enabled

Gets or sets a value indicating whether the button is enabled.

Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
ImageIndex

Gets or sets the index value of the image assigned to the button.

ImageKey

Gets or sets the name of the image assigned to the button.

Name

The name of the button.

Parent

Gets the toolbar control that the toolbar button is assigned to.

PartialPush

Gets or sets a value indicating whether a toggle-style toolbar button is partially pushed.

Pushed

Gets or sets a value indicating whether a toggle-style toolbar button is currently in the pushed state.

Rectangle

Gets the bounding rectangle for a toolbar button.

Site

Gets or sets the ISite of the Component.

(Inherited from Component)
Style

Gets or sets the style of the toolbar button.

Tag

Gets or sets the object that contains data about the toolbar button.

Text

Gets or sets the text displayed on the toolbar button.

ToolTipText

Gets or sets the text that appears as a ToolTip for the button.

Visible

Gets or sets a value indicating whether the toolbar button is visible.

Methods

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

Releases the unmanaged resources used by the ToolBarButton and optionally releases the managed resources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

Returns a string that represents the ToolBarButton control.

Events

Disposed

Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

Applies to

See also