HOW TO:判斷在 Windows Form StatusBar 控制項中按下的面板
![]() |
---|
StatusStrip 和 ToolStripStatusLabel 控制項會取代和加入功能至 StatusBar 和 StatusBarPanel 控制項;不過 StatusBar 和 StatusBarPanel 控制項會保留以提供回溯相容性和未來使用 (如果您選擇要使用)。 |
若要程式化 StatusBar 控制項 (Windows Form) 控制項來回應使用者按滑鼠的動作,要在 PanelClick 事件中使用 case 陳述式。 這個事件包含 panel 引數,其中又包含所按的 StatusBarPanel 物件之參考。 您可使用這個參考來判斷所按面板的索引,然後依此進行程式設計。
![]() |
---|
確認 StatusBar 控制項的 ShowPanels 屬性已設定為 true。 |
若要判斷按下的是哪個面板
在 PanelClick 事件處理常式中,使用 Select Case (在 Visual Basic 中) 或者 switch case (Visual C# 或 Visual C++) 陳述式,藉由驗證檢查事件引數中按下面板的索引,以決定哪個面板被滑鼠按了一下。
下列程式碼範例需要有表單、StatusBar 控制項、StatusBar1,以及兩個 StatusBarPanel 物件 ( StatusBarPanel1 和 StatusBarPanel2)。
Private Sub StatusBar1_PanelClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.StatusBarPanelClickEventArgs) Handles StatusBar1.PanelClick Select Case StatusBar1.Panels.IndexOf(e.StatusBarPanel) Case 0 MessageBox.Show("You have clicked Panel One.") Case 1 MessageBox.Show("You have clicked Panel Two.") End Select End Sub
private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e) { switch (statusBar1.Panels.IndexOf(e.StatusBarPanel)) { case 0 : MessageBox.Show("You have clicked Panel One."); break; case 1 : MessageBox.Show("You have clicked Panel Two."); break; } }
private: void statusBar1_PanelClick(System::Object ^ sender, System::Windows::Forms::StatusBarPanelClickEventArgs ^ e) { switch (statusBar1->Panels->IndexOf(e->StatusBarPanel)) { case 0 : MessageBox::Show("You have clicked Panel One."); break; case 1 : MessageBox::Show("You have clicked Panel Two."); break; } }
(Visual C#、Visual C++) 將下列程式碼加入表單的建構函式以註冊事件處理常式。
this.statusBar1.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler (this.statusBar1_PanelClick);
this->statusBar1->PanelClick += gcnew System::Windows::Forms::StatusBarPanelClickEventHandler (this, &Form1::statusBar1_PanelClick);