Export (0) Print
Expand All
Expand Minimize

ToolBarButtonClickEventHandler Delegate

Represents the method that will handle the ButtonClick event of a ToolBar.

[Visual Basic]
<Serializable>
Public Delegate Sub ToolBarButtonClickEventHandler( _
   ByVal sender As Object, _
   ByVal e As ToolBarButtonClickEventArgs _
)
[C#]
[Serializable]
public delegate void ToolBarButtonClickEventHandler(
   object sender,
   ToolBarButtonClickEventArgs e
);
[C++]
[Serializable]
public __gc __delegate void ToolBarButtonClickEventHandler(
   Object* sender,
   ToolBarButtonClickEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the ToolBarButtonClickEventHandler delegate declaration.

sender
The source of the event.
e
A ToolBarButtonClickEventArgs that contains the event data.

Remarks

When you create a ToolBarButtonClickEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about handling events with delegates, see Events and Delegates.

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

System.Windows.Forms Namespace | ToolBarButtonClickEventArgs

Show:
© 2014 Microsoft