Partager via


How to: Trigger Menu Events for Toolbar Buttons

NoteNote

The ToolStrip control replaces and adds functionality to the ToolBar control; however, the ToolBar control is retained for both backward compatibility and future use, if you choose.

If your Windows Form features a ToolBar control with toolbar buttons, you will want to know which button the user clicks.

On the ButtonClick event of the ToolBar control, you can evaluate the Button property of the ToolBarButtonClickEventArgs class. In the example below, a message box is shown, indicating which button was clicked. For details, see MessageBox Class.

The example below assumes a ToolBar control has been added to a Windows Form.

To handle the Click event on a toolbar

  1. In a procedure, add toolbar buttons to the ToolBar control.

    Public Sub ToolBarConfig()
    ' Instantiate the toolbar buttons, set their Text properties
    ' and add them to the ToolBar control.
       ToolBar1.Buttons.Add(New ToolBarButton("One"))
       ToolBar1.Buttons.Add(New ToolBarButton("Two"))
       ToolBar1.Buttons.Add(New ToolBarButton("Three"))
    ' Add the event handler delegate.
       AddHandler ToolBar1.ButtonClick, AddressOf Me.ToolBar1_ButtonClick
    End Sub
    
    public void ToolBarConfig() 
    {
       toolBar1.Buttons.Add(new ToolBarButton("One"));
       toolBar1.Buttons.Add(new ToolBarButton("Two"));
       toolBar1.Buttons.Add(new ToolBarButton("Three"));
    
       toolBar1.ButtonClick += 
          new ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
    }
    
    public void ToolBarConfig() 
    {
       toolBar1.get_Buttons().Add(new ToolBarButton("One"));
       toolBar1.get_Buttons().Add(new ToolBarButton("Two"));
       toolBar1.get_Buttons().Add(new ToolBarButton("Three"));
    
       toolBar1.add_ButtonClick(new ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick));
    }
    
    public:
       void ToolBarConfig()
       {
          toolBar1->Buttons->Add(gcnew ToolBarButton("One"));
          toolBar1->Buttons->Add(gcnew ToolBarButton("Two"));
          toolBar1->Buttons->Add(gcnew ToolBarButton("Three"));
    
          toolBar1->ButtonClick += 
             gcnew ToolBarButtonClickEventHandler(this,
             &Form1::toolBar1_ButtonClick);
       }
    
  2. Add an event handler for the ToolBar control's ButtonClick event. Use a case switching statement and the ToolBarButtonClickEventArgs class to determine the toolbar button that was clicked. Based on this, show an appropriate message box.

    NoteNote

    A message box is being used solely as a placeholder in this example. Feel free to add other code to execute when the toolbar buttons are clicked.

    Protected Sub ToolBar1_ButtonClick(ByVal sender As Object, _
    ByVal e As ToolBarButtonClickEventArgs)
    ' Evaluate the Button property of the ToolBarButtonClickEventArgs
    ' to determine which button was clicked.
       Select Case ToolBar1.Buttons.IndexOf(e.Button)
         Case 0
           MessageBox.Show("First toolbar button clicked")
         Case 1
           MessageBox.Show("Second toolbar button clicked")
         Case 2
           MessageBox.Show("Third toolbar button clicked")
       End Select
    End Sub
    
    protected void toolBar1_ButtonClick(object sender,
    ToolBarButtonClickEventArgs e)
    {
       // Evaluate the Button property of the ToolBarButtonClickEventArgs
       // to determine which button was clicked.
       switch (toolBar1.Buttons.IndexOf(e.Button))
       {
          case 0 :
             MessageBox.Show("First toolbar button clicked");
             break;
          case 1 :
             MessageBox.Show("Second toolbar button clicked");
             break;
          case 2 :
             MessageBox.Show("Third toolbar button clicked");
             break;
       }
    }
    
    protected void toolBar1_ButtonClick(System.Object sender, ToolBarButtonClickEventArgs e) 
    {
       // Evaluate the Button property of the ToolBarButtonClickEventArgs
       // to determine which button was clicked.
       switch(toolBar1.get_Buttons().IndexOf(e.get_Button())) 
       {
          case 0 : 
             MessageBox.Show("First toolbar button clicked");
             break;
          case 1 : 
             MessageBox.Show("Second toolbar button clicked");
             break;
          case 2 : 
             MessageBox.Show("Third toolbar button clicked");
             break;
       }
    }
    
    protected:
       void toolBar1_ButtonClick(System::Object ^ sender,
          ToolBarButtonClickEventArgs ^ e)
       {
         // Evaluate the Button property of the ToolBarButtonClickEventArgs
         // to determine which button was clicked.
          switch (toolBar1->Buttons->IndexOf(e->Button))
          {
             case 0 :
                MessageBox::Show("First toolbar button clicked");
                break;
             case 1 :
                MessageBox::Show("Second toolbar button clicked");
                break;
             case 2 :
                MessageBox::Show("Third toolbar button clicked");
                break;
          }
       }
    

See Also

Tasks

How to: Add Buttons to a ToolBar Control
How to: Define an Icon for a ToolBar Button

Reference

ToolBar Class

Other Resources

ToolBar Control (Windows Forms)