Export (0) Print
Expand All

How to: Add Panels to a StatusBar Control

Important note 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.

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 Note

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

    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;
    }
    
Show:
© 2014 Microsoft