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

更新:2007 年 11 月

重要说明:

StatusStripToolStripStatusLabel 控件替换了 StatusBarStatusBarPanel 控件并添加了功能;但是也可选择保留 StatusBarStatusBarPanel 控件以备向后兼容和将来使用。

若要对 StatusBar 控件(Windows 窗体) 控件进行编程以响应用户的单击操作,请在 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);
    

请参见

任务

如何:设置状态栏面板的大小

演练:在运行时更新状态栏信息

参考

StatusBar 控件概述(Windows 窗体)

StatusBar

ToolStripStatusLabel