如何触发工具栏按钮对应的菜单事件

注释

ToolStrip 控件替换并添加 ToolBar 控件的功能;但是,如果选择,则保留 ToolBar 控件以实现后向兼容性和将来使用。

如果你的 Windows 窗体中有一个ToolBar 控件,并且该控件包含工具栏按钮,你需要知道用户点击了哪个按钮。

ButtonClick 控件的 ToolBar 事件上,你可以评估 Button 类的 ToolBarButtonClickEventArgs 属性。 在下面的示例中,将显示一个消息框,指示单击了哪个按钮。 有关详细信息,请参阅 MessageBox

以下示例假定已将 ToolBar 控件添加到 Windows 窗体。

处理工具栏上的单击事件

  1. 在此过程中,向 ToolBar 控件添加工具栏按钮。

    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->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. ToolBar 控件的 ButtonClick 事件添加事件处理程序。 使用 case 开关语句和 ToolBarButtonClickEventArgs 类来确定单击的工具栏按钮。 基于此,显示适当的消息框。

    注释

    此示例中仅使用消息框作为占位符。 可以随意添加其他代码,以在单击工具栏按钮时执行。

    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->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;
          }
       }
    

另请参阅