Windows フォームにツール バー ボタンを備えた ToolBar コントロールがある場合は、ユーザーがクリックするボタンを知る必要があります。
ButtonClick コントロールのToolBar イベントでは、Button クラスのToolBarButtonClickEventArgs プロパティを評価できます。 次の例では、どのボタンがクリックされたかを示すメッセージ ボックスが表示されています。 詳細については、MessageBoxを参照してください。
次の例では、 ToolBar コントロールが Windows フォームに追加されていることを前提としています。
ツール バーの Click イベントを処理するには
プロシージャで、ツール バー ボタンを 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); }
ToolBar コントロールのButtonClick イベントのイベント ハンドラーを追加します。 case switch ステートメントと 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; } }
こちらも参照ください
- ToolBar
- 方法: ToolBar コントロールの にボタンを追加する
- 方法: ToolBar ボタン のアイコンを定義する
- ツールバー コントロール
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback