Back

TStatusBar



The StatusBar is a way for you to communicate to your program user what is happening with your program or to give the user some hints or advice.
You can add many things to a StatusBar including a graphic, the current time, the current date, hints, key status etc.

A simple and useful way to use the StatusBar is by setting the StatusBar.SimplePanel := True;
then you can use this code to show some text on the StatusBar:

Statusbar.SimpleText := 'Welcome to version 3.1 of RichText Editor!';

A StatusBar can also be used for resizing the Form it is on.

A StatusBar can have multiple panels, these are very helpful for showing many things at once, such as text, a progress bar and
the time and date.


To add the time and date to a StatusBar you can do this:

Add 2 panels to the StatusBar.
Add a Timer to Form1, then double-click on the Timer then add this code:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     with StatusBar1 do
         begin
             Panels[0].Text := (FormatDateTime('hh:mm:ss AM/PM', Now));
             Panels[1].Text := (FormatDateTime('dddddd ', Now));
         end;
end;
(Different ways of using FormatDateTime:  http://sandbrooksoftware.com/DPSC/Articles/Strings.shtml )

By using a Timer the time and date will be updated every second.
(Timer1.Interval := 1000).


Using a StatusBar to show the date, time and key status:

First you can put a TTimer component on your form, then put a StatusBar on the same Form.

Click on the StatusBar then double-click on Panels in the Object Inspector, the Panels property holds a TStatusPanels,
which is, a collection of TStatusPanel objects.
You should now see the Panels editor, you need to add 5 new Panels.

Now double-click on the TTimer component, and enter this code:

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  with StatusBar2 do
begin
  if GetKeyState(VK_CAPITAL) <> 0 then
  panels[0].Text := 'Caps Lock On'
 else
  panels[0].Text := 'Caps Lock Off';
  if GetKeyState(VK_NUMLOCK) <> 0 then
  panels[1].Text := 'Num Lock On'
 else
  panels[1].Text := 'Num Lock Off';
 if GetKeyState(VK_SCROLL) <> 0 then
  panels[2].Text := 'Scroll Lock On'
 else
  panels[2].Text := 'Scroll Lock Off';
  panels[3].Text := ' ' + (FormatDateTime('dddddd ', Now));
  panels[4].Text := ' ' + (FormatDateTime('hh:mm AM/PM', Now));
end;
end;

The GetKeyState function is used to retrieve the status of the specified virtual key, the first key we chck the status of is
the Caps Lock Key. The status specifies whether the key is up, down, or toggled.

Run your program and you should see the status of the Caps Lock key, the Num Lock key and the Scroll Lock key, also
the time and the date should be visible on the right side of the StatusBar.


To add the current keyboard keys status, you can use:

if GetKeyState(VK_NUMLOCK) <> 0 then
 Panels[1].Text := 'Num Lock On'

This is for the NumLock key.
(For more information about keys go to:  http://sandbrooksoftware.com/DPSC/Articles/keycodes.shtml )


This is a simple example we put together to that adds a TProgressBar to a TStatusBar.

First start a new project.
Add a Button to the Form somewhere at the top.
Next add a StatusBar.
Set your Form to wsMaximized.

Here is the whole unit:



unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var Form1: TForm1; I : Integer;
implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
    StatusBar1.SimplePanel := True;
    StatusBar1.SimpleText := 'Loading files... ';
    StatusBar1.Refresh;
  with TProgressBar.Create(Self) do
    try
      Top := Top + 3;
      Left := StatusBar1.Width div 2;
      Width := StatusBar1.Width div 2;
      Height := Height;
      Parent := StatusBar1;
    //  Smooth := True;
      Max := 100;
      Min := 0;
      repeat
          I := I + 1;
          Position := I;
          Sleep(10);
          Button1.Caption := IntToStr(I);
      until I >= 100;
    finally
        StatusBar1.SimpleText := 'Files loaded!';
        Free;
    end;
    I := 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     I := 0;
end;
end.


You can also add a graphic to a Statusbar, like this:

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel; const Rect: TRect);
var Pic : TBitMap;
begin
  if Panel.Style = PsOwnerDraw then
  begin
     Pic := TBitmap.Create;
     Pic.LoadFromFile('HUCKLBRY.bmp');
     StatusBar1.Canvas.FillRect(Rect);
     StatusBar1.Canvas.Draw(Rect.Left, Rect.Top, Pic);
     Pic.Free;
  end;
end;


Creating A StatusBar Panel On-The Fly!

procedure TForm1.Button1Click(Sender: TObject);
var NewPanel : TStatusPanel;
begin
   Statusbar.SimplePanel := False;
   NewPanel := StatusBar.Panels.Add;
   with NewPanel do
      begin
         Text := 'Test';
         Width := 50;
      end;
end;
Run the project and then click on Button1 a few times.


Emiel Hollander's Delphi Tips: Display hints in the statusbar

 Greg Lief's Delphi Question and Answer web page!  SETTING TSTATUSBAR COLOR

You could also use the (Free) TDFSStatusBar v1.21 component.
The main features are key lock indicators (caps, num, scroll), gauges, images, ellipsis text, and date/time.
Also, the status bar can own other components (i.e. you can drop something (Component) on it in the IDE).

Here is a screenshot of the TDFSStatusBar component in action:

The TDFSStatusBar is the bottom rectangle, notice the Netscape type color scroller(Gauge).
The top rectangle is a Panel with its color set to clBlack.



Back