Export (0) Print
Expand All

Triggering Menu Events for Toolbar Buttons

Visual Studio .NET 2003

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.
    ' Visual Basic
    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
    
    // C#
    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);
    }
    
    // C++
    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);
       }
    
  2. Add an event handler for the ToolBar control's ButtonClick event. Use a Select Case statement and the ToolBarButtonClickEventArgs class to determine the toolbar button that was clicked. Based on this, show an appropriate message box.
    Note   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.
    ' Visual Basic
    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
    
    // C#
    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;
       }
    }
    
    // C++
    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(S"First toolbar button clicked");
                break;
             case 1 :
                MessageBox::Show(S"Second toolbar button clicked");
                break;
             case 2 :
                MessageBox::Show(S"Third toolbar button clicked");
                break;
          }
       }
    

See Also

Adding Buttons to a Toolbar | Defining an Icon for a Toolbar Button | ToolBar Class | ToolBar Control (Windows Forms)

Show:
© 2014 Microsoft