如何确定 Windows 窗体状态栏中被单击的是哪个面板

重要

StatusStripToolStripStatusLabel 控件替换并添加 StatusBarStatusBarPanel 控件的功能;但是,如果选择,则保留 StatusBarStatusBarPanel 控件以实现向后兼容性和将来使用。

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

注释

确保控件StatusBarShowPanels的属性设置为 true

确定单击了哪个面板

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

    下面的代码示例要求控件窗体和两个对象的状态以及两个StatusBarStatusBar1对象 StatusBarPanel1StatusBarPanel2StatusBarPanel

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

另请参阅