Export (0) Print
Expand All

ToolBarButtonClickEventArgs Class

Provides data for the ButtonClick event.

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

System.Object
   System.EventArgs
      System.Windows.Forms.ToolBarButtonClickEventArgs

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

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

The event occurs whenever the user clicks on a button on a ToolBar control. The Button property contains the ToolBarButton wiith the information about the button that was clicked.

Example

[Visual Basic, C#, C++] The following example instantiates 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 opened. This code assumes that a Form, an OpenFileDialog, a SaveFileDialog, and a PrintDialog have been instantiated.

[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

ToolBarButtonClickEventArgs Members | System.Windows.Forms Namespace | ToolBar | ToolBarButton

Show:
© 2014 Microsoft