HOW TO:觸發工具列按鈕的功能表事件
![]() |
---|
ToolStrip 控制項會取代 ToolBar 控制項並加入其他功能,不過您也可以選擇保留 ToolBar 控制項,以提供回溯相容性及未來使用。 |
如果您的 Windows Form 具備內含幾個工具列按鈕的 ToolBar 控制項,您會需要知道使用者按的是哪一個按鈕。
在 ToolBar 控制項的 ButtonClick 事件中,您可以評估 ToolBarButtonClickEventArgs 類別的 Button 屬性。 下列範例會顯示訊息方塊,指示所按的按鈕是哪一個。 如需詳細資訊,請參閱 MessageBox 類別。
下面的範例假設已將 ToolBar 控制項加入至 Windows Form。
若要處理工具列上的 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.get_Buttons().Add(new ToolBarButton("One")); toolBar1.get_Buttons().Add(new ToolBarButton("Two")); toolBar1.get_Buttons().Add(new ToolBarButton("Three")); toolBar1.add_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 switching 陳述式和 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.get_Buttons().IndexOf(e.get_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; } }