Dela via


How to: Determine Which Panel in the Windows Forms StatusBar Control Was Clicked

Important

The StatusStrip and ToolStripStatusLabel controls replace and add functionality to the StatusBar and StatusBarPanel controls; however, the StatusBar and StatusBarPanel controls are retained for both backward compatibility and future use, if you choose.

To program the StatusBar Control (Windows Forms) control to respond to user clicks, use a case statement within the PanelClick event. The event contains an argument (the panel argument), which contains a reference to the clicked StatusBarPanel. Using this reference, you can determine the index of the clicked panel, and program accordingly.

Note

Ensure that the StatusBar control's ShowPanels property is set to true.

To determine which panel was clicked

  • In the PanelClick event handler, use a Select Case (in Visual Basic) or switch case (Visual C# or Visual C++) statement to determine which panel was clicked by examining the index of the clicked panel in the event arguments.

    The following code example requires the presence, on the form, of a StatusBar control, StatusBar1, and two StatusBarPanel objects, StatusBarPanel1 and 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++) Place the following code in the form's constructor to register the event handler.

    this.statusBar1.PanelClick += new 
       System.Windows.Forms.StatusBarPanelClickEventHandler 
       (this.statusBar1_PanelClick);
    
    this->statusBar1->PanelClick += gcnew
       System::Windows::Forms::StatusBarPanelClickEventHandler
       (this, &Form1::statusBar1_PanelClick);
    

See Also

Tasks

How to: Set the Size of Status-Bar Panels

Walkthrough: Updating Status Bar Information at Run Time

Reference

StatusBar Control Overview (Windows Forms)

StatusBar

ToolStripStatusLabel