Home

Status Bars

   

Introduction to Status Bars

 

Description

A status bar is a horizontal bar that displays in the bottom section of a form or application. 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. A status bar is a control container that is usually made of sections. The sections of status bar are called panels. The roles of the panels are left to the programmer.

Creating a Status Bar

You can create a status bar visually or programmatically. To support status bars, the VCL provides a class named TStatusBar. The TStatusBar class is derived from the TCustomStatusBar class. Both classes are defined in the ComCtrls.hpp header file.

To assist you with visually creating a status bar, the Toolbox provides a TStatusBar button in its Win32 section. You can click it and click a form. To programmatically create a status bar, declare a variable of type TStatusBar. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TStatusBar * sbStandard = new TStatusBar(this);
	sbStandard->Parent = this;
}
//---------------------------------------------------------------------------

This would produce:

Status Bar

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

Practical LearningPractical Learning: Creating a Status Bar

  1. In the Tool Palette, click Win32
  2. Click TStatusBar Status Bar and click the form
  3. While the status bar is still selected on the form, in the Object Inspector, click Name, type stbPictureViewer and press Enter

Characteristics of a Status Bar and its Panels

 

The Alignment of a Status Bar

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 Align value of a status bar is alBottom.

The Panels of a Status Bar

The sections of a status bar are called panels. Before visually creating a panel, access the Object Inspector for the status bar, click Panels, and click its button. This would display the Editing Panels window:

Editing Panels

To create a panel, click the Add New button Add New. This would create a new panel. In the same way, you can create as many panels as you want and as your status bar can show. To configure a panel, click it in the window and use the Object Inspector.

To hold or manage the panels of a status bar, the TStatusBar class is equipped with a property named Panels, which is of type TStatusPanels:

__property Comctrls::TStatusPanels * Panels = {read=FPanels,write=SetPanels};

The panels of a status bar are managed by the TStatusPanels class, which is a collection, or rather a class derived from TCollection. Each panel of a status bar, that is, each item of the TStatusPanels class is an object of type TStatusPanel. The TStatusPanel class is derived from the TCollectionItem class.

To programmatically create a panel, declare a variable of type TStatusPanel and initialize it using its constructor that is:

virtual Comctrls::TStatusPanel * __fastcall 
	TStatusPanel(Classes::TCollection * Collection);

This constructor takes a TCollection object as argument. The Collection argument specifies the list of panels to which the new panel will belong. In this cas, pass the TStatusBar::Panels property as argument. Here is an example:

ctionalities of its panels from the inherited Items property. As seen for the ToolStrip toolbar, to create the panels of a status bar:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TStatusBar * sbStandard = new TStatusBar(this);
	sbStandard->Parent = this;

	TStatusPanel * pnlItem = new TStatusPanel(sbStandard->Panels);
}
//---------------------------------------------------------------------------

In the same way, you can create as many panels as you need. To programmatically configure a panel, use the pointer provided by this declaration.

An alternative to create a panel consists of calling the AddItem() method of the TStatusPanels collection class. Its syntax is:

Comctrls::TStatusPanel * __fastcall AddItem(Comctrls::TStatusPanel * Item,
                       			    int Index);

The Item argument must be an object or reference of type TStatusPanel. The Index argument represents the position of the new panel within the collection.

You can call the TStatusPanels::AddItem() method through the TStatusBar::Panels property. Here are examples:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TStatusBar * sbStandard = new TStatusBar(this);
	sbStandard->Parent = this;

	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 0);
	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 1);
	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 2);
}
//---------------------------------------------------------------------------

Practical LearningPractical Learning: Creating the Sections of a Status Bar

  1. On the form, right-click the status bar and click Panels Editor
  2. Click the Add New button Add New
  3. In the Object Inspector, click Width, type 200 and press Enter
  4. On the Editing... window, click the Add New button Add New
  5. In the Object Inspector, click Width, type 200 and press Enter
 
 
 

Text on Panels

Most of the time, a panel is made to display text. To control this, the TStatusPanel is equipped with a property named Style, which is of type TStatusPanelStyle:

__property Comctrls::TStatusPanelStyle Style = {read=FStyle,write=SetStyle};

The TStatusPanelStyle enumeration is defined as follows:

enum TStatusPanelStyle
{
	psText,
	psOwnerDraw
};

To prepare a panel to display text, set its Style property to psText, which is the default. To let you specify the text of a panel, the TStatusPanel class is equipped with a property named Text:

__property System::UnicodeString Text = {read=FText,write=SetText};

To visually specify the text of a panel, select it in the Editing Panels window. In the Object Inspector, click Text and type the desired string. To programmatically specify the text of a panel, if you had created the panel by declaring a TStatusPanel variabe, assign a string to its Text property. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TStatusBar * sbStandard = new TStatusBar(this);
	sbStandard->Parent = this;

	TStatusPanel * pnlFirst = new TStatusPanel(sbStandard->Panels);
	TStatusPanel * pnlSecond = new TStatusPanel(sbStandard->Panels);
	TStatusPanel * pnlThird = new TStatusPanel(sbStandard->Panels);

	pnlThird->Text = L"Coordinates";
}
//---------------------------------------------------------------------------

If you don't have or don't want to use the panel's reference, you can access it by its index and assign the string to it. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	TStatusBar * sbStandard = new TStatusBar(this);
	sbStandard->Parent = this;

	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 0);
	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 1);
	sbStandard->Panels->AddItem(new TStatusPanel(sbStandard->Panels), 2);

	sbStandard->Panels[0][0]->Text = L"Ready";
}
//---------------------------------------------------------------------------

After specifying the text of a panel, you can decide how to align it, to the left (the default), the center, or the right side. This characteristic is controlled by the Alignment property;

__property Classes::TAlignment Alignment = {read=FAlignment,write=SetAlignment};

The Size of a Panel

The size of a panel mostly represents its width, which represents its length from the left to the right sides. The width of a panel depends on two factors: the total size of the status bar and the available or remaining size if/after other panels have been created.

To visually specify the width of a panel, access its Object Inspector, click Width, and type the desired value. To programmatically do this, assign an integer to the Width property of the TStatusPanel class.

The Bevel Effect of a Panel

You can make a panel appear raised, sunken, or flat. This characteristic is controlled by the Bevel property of the TStatusPanel class:

__property Comctrls::TStatusPanelBevel Bevel = {read=FBevel,write=SetBevel};

The TStatusPanelBevel enumeration has the following members:

enum TStatusPanelBevel{
	pbNone,
	pbLowered,
	pbRaised
};

Practical LearningPractical Learning: Configuring the Bevel of a Section

  1. Right-click a white area in the Editing ... window and click Add
  2. In the Object Inspector, click Bevel, click the arrow of its combo box and select pbRaised
 
 
   
 

Home Copyright © 2010-2011 FunctionX, Inc.