次の方法で共有


方法: ツール バー ボタンのメニュー イベントをトリガーする

手記

ToolStrip コントロールは、ToolBar コントロールに代わって機能を追加します。ただし、ToolBar コントロールは、下位互換性と将来の使用の両方で保持されます (選択した場合)。

Windows フォームにツール バー ボタンを含む ToolBar コントロールがある場合は、ユーザーがクリックするボタンを知る必要があります。

ToolBar コントロールの ButtonClick イベントでは、ToolBarButtonClickEventArgs クラスの Button プロパティを評価できます。 次の例では、どのボタンがクリックされたかを示すメッセージ ボックスが表示されています。 詳細については、MessageBoxを参照してください。

次の例では、ToolBar コントロールが Windows フォームに追加されていることを前提としています。

ツール バーの Click イベントを処理するには

  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 イベントのイベント ハンドラーを追加します。 ケース切り替えステートメントと 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;  
          }  
       }  
    

関連項目