如何:判斷在 Windows Form StatusBar 控制項中按下的面板
重要
StatusStrip 和 ToolStripStatusLabel 控制項會取代和新增 StatusBar 和 StatusBarPanel 控制項的功能;不過,如果您選擇的話,則可保留 StatusBar 和 StatusBarPanel 控制項,以提供回溯相容性及方便日後使用。
若要對 StatusBar 控制項控制項進行程式設計來回應使用者點擊,請使用 PanelClick 事件內的 case 陳述式。 事件包含引數 (面板引數),其包含已按下 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);