This documentation is archived and is not being maintained.

Determining Which Panel in the Windows Forms StatusBar Control Was Clicked

Visual Studio .NET 2003

To program the StatusBar control to respond to user clicks, use the Select Case statement within the PanelClick event. The event contains an argument (the panel argument), which contains a reference to the clicked StatusBarPanel object. 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. For more information, see Setting Properties for Controls, Documents, and Forms.

To determine which panel was clicked

  • In the PanelClick event handler, use a Select Case statement to determine which panel was clicked by examining the index of the clicked panel in the event arguments.

    The code below assumes the presence, on the form, of a StatusBar control, StatusBar1, and two StatusBarPanels, StatusBarPanel1 and StatusBarPanel2.

    ' Visual Basic
    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
    
    // C#
    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;
       }
    }
    
    // C++
    private:
       System::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# and Visual C++ Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.statusBar1.PanelClick += new
    System.Windows.Forms.StatusBarPanelClickEventHandler
    (this.statusBar1_PanelClick);
    // C++
    this->statusBar1->PanelClick += new
    System::Windows::Forms::StatusBarPanelClickEventHandler
    (this, statusBar1_PanelClick);

See Also

Setting the Size of Status-Bar Panels | Walkthrough: Updating Status-Bar Information at Run Time | Introduction to the Windows Forms StatusBar Control | StatusBar Class

Show: