* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
5.5 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Trigger Menu Events for Toolbar Buttons | 03/30/2017 |
|
|
98374f70-993d-4ca4-89fb-48fea6ce5b45 |
How to: Trigger Menu Events for Toolbar Buttons
Note
The xref:System.Windows.Forms.ToolStrip control replaces and adds functionality to the xref:System.Windows.Forms.ToolBar control; however, the xref:System.Windows.Forms.ToolBar control is retained for both backward compatibility and future use, if you choose.
If your Windows Form features a xref:System.Windows.Forms.ToolBar control with toolbar buttons, you will want to know which button the user clicks.
On the xref:System.Windows.Forms.ToolBar.ButtonClick event of the xref:System.Windows.Forms.ToolBar control, you can evaluate the xref:System.Windows.Forms.ToolBarButtonClickEventArgs.Button%2A property of the xref:System.Windows.Forms.ToolBarButtonClickEventArgs class. In the example below, a message box is shown, indicating which button was clicked. For details, see xref:System.Windows.Forms.MessageBox.
The example below assumes a xref:System.Windows.Forms.ToolBar control has been added to a Windows Form.
To handle the Click event on a toolbar
-
In a procedure, add toolbar buttons to the xref:System.Windows.Forms.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 Subpublic 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->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); } -
Add an event handler for the xref:System.Windows.Forms.ToolBar control's xref:System.Windows.Forms.ToolBar.ButtonClick event. Use a case switching statement and the xref:System.Windows.Forms.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.
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 Subprotected 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->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; } }