If the SimplePanel property is set to True, the TStatusBar component can only display a single piece of information specified in the SimpleText property. But if the SimplePanel property is set to False, we can use the Panels property to create a multipanel status bar and display more information to the user.
Select the status bar component and then click the (…) button next to its Panels property to display the Collection Editor. Click the Add New button three times to create three status bar panels. Change the Width of the first two panels to 75 to make more room for text.
We'll use the first panel to display how many lines are in the document and which line is currently selected. The second panel will be used to display "Modified" if the contents of the editor are changed, and the last panel will be used to display the hint.
The best place for this code is in the OnIdle event of the application, so add a TApplicationEvents component to the Designer Surface and write the following code in the OnIdle event handler:
procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); const MODIFIED: array[Boolean] of string = ('', 'Modified'); begin { line status } StatusBar1.Panels[0].Text := Format('Line %d/%d', [Succ(Editor.CaretPos.Y), Succ(Editor.Lines.Count)]); { modified status } StatusBar1.Panels[1].Text := MODIFIED[Editor.Modified]; end;
If you're using the C++ language, here's what your OnIdle event handler should look like:
void __fastcall TMainForm::ApplicationEvents1Idle(TObject *Sender, bool &Done) { const AnsiString MODIFIED[] = {"", "Modified"}; StatusBar1->Panels->Items[0]->Text = Format("Line %d/%d", ARRAYOFCONST((Editor->CaretPos.y + 1, Editor->Lines->Count + 1))); StatusBar1->Panels->Items[1]->Text = MODIFIED[Editor->Modified]; }
To display the hint in the last panel, write a handler for the OnHint event:
procedure TMainForm.ApplicationEvents1Hint(Sender: TObject); begin StatusBar1.Panels[2].Text := Application.Hint; end;
If you've done everything right, at run time your status bar should look like the one displayed in Figure 16-16.
Figure 16-16: The status bar