Formatting toolbars
The font and paragraph toolbars are the two formatting bars. Both follow the same
pattern: an Options set chooses which buttons are visible, per-action OnApply*
events fire after the toolbar applies a change to the connected editor, and a
Settings object reflects the editor's current state so you can read or push it
in code. Use this chapter to tailor which controls appear and to react to the
user's formatting choices.
Font toolbar
TTMSFNCFontToolBar.Options is a set of TTMSFNCFontToolBarOption values
(ftoFontName, ftoFontSize, ftoBold, ftoItalic, ftoUnderline,
ftoStrikeout, ftoTextColor, ftoBackgroundColor); it defaults to
AllFontOptions. Drop options you do not need, and handle the OnApply* events
to mirror the choice elsewhere in the UI.
// Control which buttons the font toolbar shows and handle apply events
TMSFNCFontToolBar1.Options :=
[ftoFontName, ftoFontSize, ftoBold, ftoItalic, ftoUnderline, ftoTextColor];
// Respond when the user picks a different font name
procedure TForm1.TMSFNCFontToolBar1ApplyFontName(Sender: TObject; AFontName: String);
begin
StatusBar1.Panels.Items[0].Text := 'Font: ' + AFontName;
end;
// Respond when the user picks a different font size
procedure TForm1.TMSFNCFontToolBar1ApplyFontSize(Sender: TObject; AFontSize: Integer);
begin
StatusBar1.Panels.Items[1].Text := 'Size: ' + IntToStr(AFontSize);
end;
Reading and setting state
Settings exposes Bold, Italic, Underline, Strikeout, Font and
BackgroundColor. Read it to keep a status display in sync; assign to it to push
formatting into the editor selection without clicking a button.
// Read and set the font state the font toolbar reflects through its Settings.
// Reading keeps a status display in sync; writing pushes formatting to the
// bound editor selection.
procedure TForm1.ShowFontState;
begin
// Read the current selection's formatting.
if TMSFNCFontToolBar1.Settings.Bold then
StatusBar1.Panels.Items[0].Text := 'Bold';
// Push a state into the toolbar and the bound editor selection.
TMSFNCFontToolBar1.Settings.Italic := True;
TMSFNCFontToolBar1.Settings.Font.Name := 'Segoe UI';
TMSFNCFontToolBar1.Settings.BackgroundColor := gcYellow;
end;
Paragraph toolbar
TTMSFNCParagraphToolBar.Options is a set of TTMSFNCParagraphToolBarOption
values (ptoAlignLeft, ptoAlignCenter, ptoAlignRight, ptoBullets,
ptoNumbering, ptoUnindent, ptoIndent). Its Settings object exposes the
current Align (a TAlignment) and BulletIndex, and its events cover
alignment, indent/unindent, numbering and bullet selection.
// A paragraph toolbar: choose which buttons are visible, react to alignment and
// bullets, and read the current paragraph state from Settings.
procedure TForm1.FormCreate(Sender: TObject);
begin
TMSFNCParagraphToolBar1.Control := TMSFNCRichEditor1;
TMSFNCParagraphToolBar1.Options :=
[ptoAlignLeft, ptoAlignCenter, ptoAlignRight, ptoBullets, ptoNumbering];
end;
procedure TForm1.TMSFNCParagraphToolBar1AlignCenter(Sender: TObject);
begin
StatusBar1.Panels.Items[0].Text := 'Centered';
end;
procedure TForm1.TMSFNCParagraphToolBar1ApplyBullets(Sender: TObject; AIndex: Integer);
begin
// AIndex identifies which bullet style was chosen from the picker.
StatusBar1.Panels.Items[0].Text := 'Bullet style ' + IntToStr(AIndex);
end;
procedure TForm1.ButtonReadStateClick(Sender: TObject);
begin
// Settings reflects the editor's current paragraph alignment and bullet style.
if TMSFNCParagraphToolBar1.Settings.Align = taCenter then
Memo1.Lines.Add('Paragraph is centered');
end;
Combining font and paragraph toolbars
Putting it together — both toolbars on one editor, with a bullet handler and a
manual read of the font Settings for a status display:
// Combine font and paragraph toolbars on the same editor.
// Reading the current state from Settings lets you keep a status display
// in sync without querying the editor directly.
procedure TForm1.FormCreate(Sender: TObject);
begin
TMSFNCFontToolBar1.Control := TMSFNCRichEditor1;
TMSFNCParagraphToolBar1.Control := TMSFNCRichEditor1;
end;
procedure TForm1.TMSFNCParagraphToolBar1ApplyBullets(Sender: TObject; AIndex: Integer);
begin
// AIndex identifies which bullet style was chosen from the picker
LabelBullet.Caption := 'Bullet style: ' + IntToStr(AIndex);
end;
// Manually read current font settings from the toolbar's Settings object
procedure TForm1.ButtonQueryClick(Sender: TObject);
begin
with TMSFNCFontToolBar1.Settings do
Memo1.Lines.Add(Format('Bold=%s Italic=%s Font=%s',
[BoolToStr(Bold, True), BoolToStr(Italic, True), Font.Name]));
end;
Common mistakes
- Reducing
Optionsto hide a feature you still handle. Removing an option hides its button and its action — keep the option if the event still needs to fire. - Writing to
Settingswith noControl.Settingsreflects and drives the bound editor; with noControlthere is nothing to apply the change to.