Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

2. Windows Forms Controls

2.37 How do I determine which button in a Toolbar is clicked?


When you click on a button on a Toolbar the click is sent to the Toolbar, so you need to check the ToolBarButtonClickEventArgs's button property to determine the button on the Toolbar that was clicked.

[C#]
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
          {
               //check if toolBarButton1 was clicked
               if (e.Button == toolBarButton1)
               {
                    MessageBox.Show("Button 1 clicked");
               }
          }

[VB.NET]
Private Sub toolBar1_ButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs)
               'check if toolBarButton1 was clicked
               If e.Button = toolBarButton1 Then
                    MessageBox.Show("Button 1 clicked")
               End If
          End Sub