Udostępnij za pośrednictwem


How to: Add Panels to a StatusBar Control

Important noteImportant Note:

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.

The programmable area within a StatusBar Control (Windows Forms) control consists of instances of the StatusBarPanel class. These are added through additions to the StatusBar.StatusBarPanelCollection class.

To add panels to a status bar

  • In a procedure, create status-bar panels by adding them to the StatusBar.StatusBarPanelCollection. Specify property settings for individual panels by using its index passed through the Panels property.

    In the following code example, the path set for the location of the icon is the My Documents folder. This location is used because you can assume that most computers running the Windows operating system will include this folder. Choosing this location also allows users with minimal system access levels to safely run the application. The following example requires a form with a StatusBar control already added.

    Note

    The StatusBar.StatusBarPanelCollection is a zero-based collection, so code should proceed accordingly.

    Public Sub CreateStatusBarPanels()
    ' Create panels and set text property.
       StatusBar1.Panels.Add("One")
       StatusBar1.Panels.Add("Two")
       StatusBar1.Panels.Add("Three")
    ' Set properties of StatusBar panels.
    ' Set AutoSize property of panels.
       StatusBar1.Panels(0).AutoSize = StatusBarPanelAutoSize.Spring
       StatusBar1.Panels(1).AutoSize = StatusBarPanelAutoSize.Contents
       StatusBar1.Panels(2).AutoSize = StatusBarPanelAutoSize.Contents
    ' Set BorderStyle property of panels.
       StatusBar1.Panels(0).BorderStyle = StatusBarPanelBorderStyle.Raised
       StatusBar1.Panels(1).BorderStyle = StatusBarPanelBorderStyle.Sunken
       StatusBar1.Panels(2).BorderStyle = StatusBarPanelBorderStyle.Raised
    ' Set Icon property of third panel. You should replace the bolded
    ' icon in the sample below with an icon of your own choosing.
       StatusBar1.Panels(2).Icon = New _ 
       System.Drawing.Icon(System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Icon.ico")
       StatusBar1.ShowPanels = True
    End Sub
    
    public void CreateStatusBarPanels()
    {
       // Create panels and set text property.
       statusBar1.Panels.Add("One");
       statusBar1.Panels.Add("Two");
       statusBar1.Panels.Add("Three");
       // Set properties of StatusBar panels.
       // Set AutoSize property of panels.
       statusBar1.Panels[0].AutoSize = StatusBarPanelAutoSize.Spring;
       statusBar1.Panels[1].AutoSize = StatusBarPanelAutoSize.Contents;
       statusBar1.Panels[2].AutoSize = StatusBarPanelAutoSize.Contents;
       // Set BorderStyle property of panels.
       statusBar1.Panels[0].BorderStyle =
          StatusBarPanelBorderStyle.Raised;
       statusBar1.Panels[1].BorderStyle = StatusBarPanelBorderStyle.Sunken;
       statusBar1.Panels[2].BorderStyle = StatusBarPanelBorderStyle.Raised;
       // Set Icon property of third panel. You should replace the bolded
       // icon in the sample below with an icon of your own choosing.
       // Note the escape character used (@) when specifying the path.
       statusBar1.Panels[2].Icon = 
          new System.Drawing.Icon (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       + @"\Icon.ico");
       statusBar1.ShowPanels = true;
    }
    
    public:
       void CreateStatusBarPanels()
       {
          // Create panels and set text property.
          statusBar1->Panels->Add("One");
          statusBar1->Panels->Add("Two");
          statusBar1->Panels->Add("Three");
          // Set properties of StatusBar panels.
          // Set AutoSize property of panels.
          statusBar1->Panels[0]->AutoSize =
             StatusBarPanelAutoSize::Spring;
          statusBar1->Panels[1]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          statusBar1->Panels[2]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          // Set BorderStyle property of panels.
          statusBar1->Panels[0]->BorderStyle =
             StatusBarPanelBorderStyle::Raised;
          statusBar1->Panels[1]->BorderStyle =
             StatusBarPanelBorderStyle::Sunken;
          statusBar1->Panels[2]->BorderStyle =
             StatusBarPanelBorderStyle::Raised;
          // Set Icon property of third panel.
          // You should replace the bolded image 
          // in the sample below with an icon of your own choosing.
          statusBar1->Panels[2]->Icon =
             gcnew System::Drawing::Icon(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             "\\Icon.ico"));
          statusBar1->ShowPanels = true;
       }
    

See Also

Tasks

How to: Set the Size of Status-Bar Panels

Walkthrough: Updating Status Bar Information at Run Time

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

Reference

Collection Editor Dialog Box

StatusBar Control Overview (Windows Forms)

StatusBar

ToolStripStatusLabel