英語で読む

次の方法で共有


ToolBarButtonClickEventHandler 代理人

定義

ButtonClickToolBar イベントを処理するメソッドを表します。

C#
public delegate void ToolBarButtonClickEventHandler(object sender, ToolBarButtonClickEventArgs e);

パラメーター

sender
Object

イベントのソース。

e
ToolBarButtonClickEventArgs

イベント データを格納している ToolBarButtonClickEventArgs

次の例では、 ToolBar コントロールと 3 つの ToolBarButton コントロールをインスタンス化します。 ツール バー ボタンはボタン コレクションに割り当てられ、コレクションはツール バーに割り当てられ、ツール バーはフォームに追加されます。 ツール バーの ButtonClick イベントで、 ButtonToolBarButtonClickEventArgs プロパティが評価され、適切なダイアログが開きます。 このコードでは、 Form、、、 OpenFileDialogSaveFileDialogPrintDialog がインスタンス化されていることを前提としています。

C#
public void InitializeMyToolBar()
 {
    // Create and initialize the ToolBar and ToolBarButton controls.
    toolBar1 = new ToolBar();
    ToolBarButton toolBarButton1 = new ToolBarButton();
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = new ToolBarButton();
 
    // Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open";
    toolBarButton2.Text = "Save";
    toolBarButton3.Text = "Print";
 
    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
    
    // Add the event-handler delegate.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
       this.toolBar1_ButtonClick);
    
    // Add the ToolBar to the Form.
    Controls.Add(toolBar1);
 }
 
 private void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
 {
   // Evaluate the Button property to determine which button was clicked.
   switch(toolBar1.Buttons.IndexOf(e.Button))
   {
      case 0:
         openFileDialog1.ShowDialog();
         // Insert code to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert code to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert code to print the file.    
         break; 
    }
 }

注釈

ToolBarButtonClickEventHandler デリゲートを作成する場合は、イベントを処理するメソッドを指定します。 イベント ハンドラーにイベントを関連付けるには、イベントにデリゲートのインスタンスを追加します。 イベント ハンドラーは、デリゲートを削除しない限り、イベントが発生するたびに呼び出されます。 デリゲートを使用したイベントの処理の詳細については、「イベントの 処理と発生」を参照してください。

拡張メソッド

GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0

こちらもご覧ください