Export (0) Print
Expand All

Adding Panels to a StatusBar Control

Visual Studio .NET 2003

The programmable area within a StatusBar control consists of instances of the StatusBarPanel class. These are added at design time through the StatusBarPanel Collection Editor, and at run time through additions to the StatusBarPanelCollection class.

To add panels to a status bar at design time

  1. Add a StatusBar control to the form. For details, see Adding Controls to Windows Forms.
  2. In the Properties window, click the Panels property to select it. Then click the Ellipsis ( Aa984273.vbellipsesbutton(en-us,VS.71).gif ) button to open the StatusBarPanel Collection Editor.
  3. Use the Add and Remove buttons to add and remove panels from the StatusBar control. Configure the properties of the individual panels in the properties window that appears in the pane on the right side. Important properties to consider include:
    PropertyDescription
    AutoSizeDetermines the resize behavior of the panel. Must be one of the values of the StatusBarPanelAutoSize enumeration.
    AlignmentSets the alignment of the panel within the StatusBar control. Must be one of the values of the HorizontalAlignment enumeration.
    BorderStyleThe type of border displayed at the edges of the panel. Must be one of the values of the StatusBarPanelBorderStyle enumeration.
    IconThe icon (.ico file) displayed within the panel.
    StyleSets the style of the panel. Must be one of the values of the StatusBarPanelStyle enumeration.
    TextThe text string displayed by the panel.
    MinWidthThe minimum width the panel can be within the status bar.
  4. Click OK to close the dialog box and create the panels you specified.
  5. In the Properties window, set the ShowPanels property to true.

To add panels to a status bar programmatically

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

    In the example below, 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 example below assumes a form with a StatusBar control already added.

    Note   The StatusBarPanels collection is a zero-based collection, so code should proceed accordingly.
    ' Visual Basic
    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
    
    // C#
    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;
    }
    
    // C++
    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->Item[0]->AutoSize =
             StatusBarPanelAutoSize::Spring;
          statusBar1->Panels->Item[1]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          statusBar1->Panels->Item[2]->AutoSize =
             StatusBarPanelAutoSize::Contents;
          // Set BorderStyle property of panels.
          statusBar1->Panels->Item[0]->BorderStyle =
             StatusBarPanelBorderStyle::Raised;
          statusBar1->Panels->Item[1]->BorderStyle =
             StatusBarPanelBorderStyle::Sunken;
          statusBar1->Panels->Item[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->Item[2]->Icon =
             new System::Drawing::Icon(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             S"\\Icon.ico"));
          statusBar1->ShowPanels = true;
       }
    

See Also

Collection Editor Dialog Box | Setting the Size of Status-Bar Panels | Walkthrough: Updating Status-Bar Information at Run Time | Determining Which Panel in the Windows Forms StatusBar Control Was Clicked | Introduction to the Windows Forms StatusBar Control | StatusBar Class

Show:
© 2015 Microsoft