如何:确定 Windows 窗体 StatusBar 控件中被单击的面板

重要

StatusStripToolStripStatusLabel 控件取代了 StatusBarStatusBarPanel 控件并向其中添加了新功能;但也可选择保留 StatusBarStatusBarPanel 控件以备后向兼容和供将来使用。

若要对 StatusBar 控件进行编程以响应用户单击,请在 PanelClick 事件中使用 case 语句。 该事件包含一个参数(面板参数),其中包含对单击的 StatusBarPanel 的引用。 使用此引用,可以确定单击面板的索引,并进行相应的编程。

注意

确保 StatusBar 控件的 ShowPanels 属性设置为 true

确定所单击的面板

  1. PanelClick 事件处理程序中,使用 Select Case 语句(采用 Visual Basic 时)或 switch case 语句(采用 Visual C# 或 Visual C++ 时)通过检查事件参数中单击面板的索引来确定单击了哪个面板。

    下面的代码示例要求在窗体上有一个 StatusBar 控件 (StatusBar1) 以及两个 StatusBarPanel 对象(StatusBarPanel1StatusBarPanel2)。

    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);  
    

另请参阅