Home

Applications Accessories: Status Bars

 

Introduction to Status Bars

 

Description

A status bar is a horizontal host that displays in the bottom section of a form. The original purpose of the status bar was to display messages to the user. Modern status bars have seen their roles expanded. Still, the primary job a status bar is to show some text depending on what is going on in the application.

Here is an example of a status bar that shows the coordinate location of a mouse inside a drawing board of a paint program:

Status Bar

A status bar is a control container that is usually made of sections. The sections of a status bar are called panels. The roles of the panels are left to the programmer.

ApplicationPractical Learning: Introducing MDI Applications

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list,click Windows Forms Application
  4. Change the Name to Notice1 and click OK
  5. From the Dialogs section of the Toolbox, click the OpenFileDialog button OpenFileDialog and click the form
  6. In the Properties window, click (Name) and type dlgFileOpen
  7. From the Menus & Toolbars section of the Toolbox, click the MenuStrip button OpenFileDialog and click the form
  8. In the Properties window, change its (Name) to mnuMain
  9. Under the form, right-click the menu strip and click Insert Standard Items
  10. Under the form, click mnuMain. In the Properties window, click Items and click its browse button
  11. Change the names of the items as follows:

    Top Menu Item DropDownItems
    Old Name New Name Old Name New Name
    fileToolStripMenuItem mnuFile    
        newToolStripMenuItem mnuFileNew
        openToolStripMenuItem mnuFileOpen
          Separator
        saveToolStripMenuItem mnuFileSave
        saveAsToolStripMenuItem mnuFileSaveAs
          Separator
        printToolStripMenuItem mnuFilePrint
        printPreviewToolStripMenuItem mnuFilePrintPreview
          Separator
        exitToolStripMenuItem mnuFileExit
    editToolStripMenuItem mnuEdit    
        undoToolStripMenuItem mnuEditUndo
        redoToolStripMenuItem mnuEditRedo
          Separator 
        cutToolStripMenuItem mnuEditCut
        copyToolStripMenuItem mnuEditCopy
        pasteToolStripMenuItem mnuEditPaste 
          Separator 
        selectAllToolStripMenuItem mnuEditSelectAll
    toolsToolStripMenuItem mnuTools    
        customizeToolStripMenuItem mnuToolsCustomize
        optionsToolStripMenuItem mnuToolsOptions
    helpToolStripMenuItem mnuHelp    
        contentsToolStripMenuItem mnuHelpContents
        indexToolStripMenuItem mnuHelpIndex
        searchToolStripMenuItem mnuHelpSearch
          Separator
        aboutToolStripMenuItem mnuHelpAbout
  12. Click OK and OK
  13. From the Menus & Toolbars section of the Toolbox, click ToolStrip Tool Strip and click the form
  14. While the tool strip is still selected, in the Properties window, change its Name to tbrStandard
  15.  
  16. On the form, right-click the tool strip and click Insert Standard Items
  17. Under the form, click tbrStandard. Under the Properties window, click Edit Items
  18. Change the names of the items as follows:
     
    Old Name New Name
    newToolStripButton tbrStandardNew
    openToolStripButton tbrStandardOpen
    saveToolStripButton tbrStandardSave
    printToolStripButton tbrStandardPrint
    Separator  
    cutToolStripButton tbrStandardCut
    copyToolStripButton tbrStandardCopy
    pasteToolStripButton tbrStandardPaste
    Separator  
    helpToolStripButton tbrStandardHelp

Creating a Status Bar

You can create a status bar visually or programmatically. To support status bars, the .NET Framework provides a class named StatusStrip. The StatusStrip class is derived from the ToolStrip class, from which it gets its primary functionality.

Author Note

The previous versions of the .NET Framework, namely 1.0, included a class named StatusBar used to create a status bar. That class is still available and you can use it if you want. In our lessons, unless specified otherwise, we will use the StatusStrip class or the StatusStrip object from the Toolbox.

To assist you with visually creating a status bar, the Toolbox provides a StatusStrip button in its Menus & Toolbars section. You can click it and click a form. To programmatically create a status bar, declare a variable of type StatusStrip and add it to the Controls collection of the form. Here is an example:

#include <windows.h>

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

public ref class CExercise : public Form
{
private:
    StatusStrip ^ statusbar;

public:
    CExercise()
    {
	InitializeComponent();
    }

private:
    void InitializeComponent()
    {
        statusbar = gcnew StatusStrip;

        Controls->Add(statusbar);
        
        Text = "Exercise";
        StartPosition = FormStartPosition::CenterScreen;
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise());

    return 0;
}

This would produce:

Toolbar

If you have a good reason to do so, you can add more than one status bar to a form.

 
 
 

ApplicationPractical Learning: Creating a Status Bar

  1. From the Menus & Toolbars section of the Toolbox, click StatusStrip Status Strip and click the form
  2. While the tool strip is still selected, in the Properties window, change its Name to stbApplicationStatus

Characteristics of a Status Bar

 

Introduction

A status bar can be used for its aesthetic characteristics as it can be made to display sunken or raised bars to the bottom of a form:

Status Bar

Other than that, a status bar can be made to display other items.

Like a toolbar, a status bar is an intermediary container, meaning it must be positioned on another container, which is usually a form. The default Dock value of a status bar is Bottom.

The Items on a Status Bar

To manage its panels, the StatusStrip class inherits the functionalities of its panels from the inherited Items property. As seen for the ToolStrip ^ toolbar, to create the panels of a status bar, do one of the following:

  • Click the status bar to select it on the form. Click the arrowed button on its top-right side and click Edit Items...
  • Right-click the status bar and click Edit Items...
  • Click the status bar on the form to select it. Then, under the Properties window, click Edit Items...

This would open the Items Collection Editor. Use it to add the desired items that would then create the panels.

Like a toolbar, a status bar can host some items. A status bar is primarily used to display text. To get such text, you can create a label and add it to the status bar. A label on a status bar is an object of type ToolStripStatusLabel. The ToolStripStatusLabel class inherits from a class named ToolStripLabel.

To visually add a label to a status bar:

  • Click an empty area on the status bar to create a new button. Click the arrow of the new button and click StatusLabel

    Status Bar

  • Display the Properties window for the status bar. In Select Item And Add To List Below, select StatusLabel and click Add

To programmatically add a label to a status bar, declare a variable of type ToolStripStatusLabel, initialize it, and add it to the Items property of the StatusStrip variable. Here is an example:

#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public ref class CExercise : public Form
{
private:
    StatusStrip ^ statusbar;
    ToolStripStatusLabel ^ lblMessage;

public:
    CExercise()
    {
	InitializeComponent();
    }

private:
    void InitializeComponent()
    {
        statusbar = gcnew StatusStrip;
        
        lblMessage = gcnew ToolStripStatusLabel;
        statusbar->Items->Add(lblMessage);

        Controls->Add(statusbar);
        Text = L"Exercise";
        StartPosition = FormStartPosition::CenterScreen;
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise());

    return 0;
}

Like the label of a toolbar, the label of a status bar can be made to display text, an icon, or both. This is handled by the DisplayStyle property that has the same options as the other.

The label of a status bar is highly configurable. It has the ability to sink or raise its borders. If you want to control the borders of a label, first use its BorderSides property:

Border Sides

You can do this programmatically as follows:

void InitializeComponent()
{
    statusbar = gcnew StatusStrip;
    lblMessage = gcnew ToolStripStatusLabel;
    lblMessage->BorderSides = ToolStripStatusLabelBorderSides::All;

    statusbar->Items->Add(lblMessage);
    Controls->Add(statusbar);
}

After setting the BorderSides  property, select the type of border you want in the BorderStyle property:

Border Style

You can also specify the border style programmatically. Here is an example:

void InitializeComponent()
{
    statusbar = gcnew StatusStrip();
    
    lblMessage = gcnew ToolStripStatusLabel();
    lblMessage->AutoSize = false;
    lblMessage->Width = 125;
    lblMessage->BorderSides = ToolStripStatusLabelBorderSides::All;
    lblMessage->BorderStyle = Border3DStyle::Sunken;

    statusbar->Items->Add(lblMessage);
    Controls->Add(statusbar);
}

This would produce:

Border Sides

Besides the label, a status bar can also contain a drop down button, a split button, and/or a progress bar.

ApplicationPractical Learning: Populating a Status Bar

  1. On the form, to create a section, click the button that displays
  2. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): pnlMessage
    AutoSize: False
    BorderSides: All
    BorderStyle: Sunken
    DisplayStyle: None
    Size -> Width: 200
  3. On the form, click the new button on the right side of the previous label
  4. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): pnlStatus
    AutoSize: False
    BorderSides: All
    BorderStyle: Raised
    DisplayStyle: None
    Size -> Width: 150
  5. On the form, click the new button on the right side of the previous label (you should now have three labels)
  6. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): pnlZoom
    BorderSides: All
    BorderStyle: SunkenOuter
    DisplayStyle: None
    Spring: True
     
    Status Bar
  7. Close Microsoft Visual Studio
  8. When asked whether you want to save, click Discard
 
 
   
 

Home Copyright © 2010-2012 FunctionX