--- title: "How to: Trigger Menu Events for Toolbar Buttons" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "examples [Windows Forms], toolbars" - "ToolBar control [Windows Forms], click event handlers" - "ToolBar control [Windows Forms], coding button click events" - "toolbars [Windows Forms], click event handlers" ms.assetid: 98374f70-993d-4ca4-89fb-48fea6ce5b45 --- # How to: Trigger Menu Events for Toolbar Buttons > [!NOTE] > The control replaces and adds functionality to the control; however, the control is retained for both backward compatibility and future use, if you choose. If your Windows Form features a control with toolbar buttons, you will want to know which button the user clicks. On the event of the control, you can evaluate the property of the class. In the example below, a message box is shown, indicating which button was clicked. For details, see . The example below assumes a 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 control. ```vb 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 ``` ```csharp 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); } ``` ```cpp 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 control's event. Use a case switching statement and the 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. ```vb 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 ``` ```csharp 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; } } ``` ```cpp 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 - - [How to: Add Buttons to a ToolBar Control](how-to-add-buttons-to-a-toolbar-control.md) - [How to: Define an Icon for a ToolBar Button](how-to-define-an-icon-for-a-toolbar-button.md) - [ToolBar Control](toolbar-control-windows-forms.md)