Tips and Frequently Asked Questions
Color text on the fly while typing
It is possible to check rules after the user types a word in TAdvRichEditor and based on this, apply formatting to the entered text.
This sample code snippet will apply a red color when the user has entered a number value in the TAdvRichEditor:
procedure TForm1.AdvRichEditor1EnterWord(Sender: TObject; AWord: string);
var
v,e,ci,ss,sl: integer;
el: TREElement;
begin
val(aword, v, e);
if (e = 0) then
begin
// is a number
// store current selection
ss := AdvRichEditor1.SelStart;
sl := AdvRichEditor1.SelLength;
ci := AdvRichEditor1.Caret.CharIndex;
if AdvRichEditor1.Caret.Element.Text[ci] = '' '' then
dec(ci);
// set selection to entered word to apply new color
AdvRichEditor1.Selection.FromElement := AdvRichEditor1.Caret.Element;
AdvRichEditor1.Selection.FromChar := ci - Length(AWord);
AdvRichEditor1.Selection.ToElement := AdvRichEditor1.Caret.Element;
AdvRichEditor1.Selection.ToChar := ci;
AdvRichEditor1.SetSelectionColor(clRed);
// restore current selection
AdvRichEditor1.SelStart := ss;
AdvRichEditor1.SelLength := 0;
AdvRichEditor1.SelectionToCaret;
AdvRichEditor1.SetSelectionColor(clBlack);
end;
end;
Controlling clipboard format
You can use the AdvRichEditor.ClipboardFormat property to control what clipboard formats are accepted for TAdvRichEditor.
The ClipboardFormat is a set of possible values:
TClipboardFormat = (cfRTE, cfRTF, cfText, cfHTML, cfBMP, cfFile);
To accept only plain text for example to paste in TAdvRichEditor, use:
AdvRichEditor.ClipboardFormat := [cfText];
Use TAdvRichEditor as logging tool
A technique that can be used to automatically scroll to the last line when programmatically adding lines for a logging system, is to set the caret to the last line with the SetCaret() method and then call AdvRichEditor.ScrollToCaret;
Sample code:
begin
advricheditor1.AddMultiLineText(''a new line ''+inttostr(t));
advricheditor1.AddLineBreak;
inc(t);
advricheditor1.SetCaret(cpEndDoc);
advricheditor1.ScrollToCaret;
end;
Customizing the popup menu
From the OnContextPopup event that is triggered before the popup menu is displayed, it is easy to customize it, for example to dynamically translate or add items.
This example performs a dynamic translation:
procedure TForm4.AdvRichEditor1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
if AppLanguage = alGerman then
begin
AdvRichEditor1.PopupMenu.Items[0].Caption := ''Löschen'';
AdvRichEditor1.PopupMenu.Items[2].Caption := ''Ausschneiden'';
AdvRichEditor1.PopupMenu.Items[3].Caption := ''Kopieren'';
AdvRichEditor1.PopupMenu.Items[4].Caption := ''Einfügen'';
AdvRichEditor1.PopupMenu.Items[6].Caption := ''Ausrichten'';
AdvRichEditor1.PopupMenu.Items[6].Items[0].Caption := ''Links'';
AdvRichEditor1.PopupMenu.Items[6].Items[1].Caption := ''Zentrum'';
AdvRichEditor1.PopupMenu.Items[6].Items[2].Caption := ''Rechts'';
end;
end;
How to use a watermark
It is easy to use a watermark for TAdvRichEditor. To do this, simply implement the OnDrawBackground event and in this event, you can use the canvas to draw whatever watermark you may want for the TAdvRichEditor.
Example:
var
png: TPNGImage;
procedure TForm4.FormCreate(Sender: TObject);
begin
png := TPNGImage.Create;
png.LoadFromFile(''e:\tms\temp\watermark-news.png'');
end;
procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
png.Free;
end;
procedure TForm4.AdvRichEditor1DrawBackground(Sender: TObject; ACanvas: TCanvas;
ARect: TRect);
begin
ACanvas.Draw(0,0,png);
end;
How to add color & font style formatted text
Adding color & font style formatted text to TAdvRichEditor can be done with setting a selection and calling AdvRichEditor.SetSelectionColor() or AdvRichEditor.SetSelectionBold(), etc.. but it can also be done by changing the AdvRichEditor.Font before adding new text.
Sample code:
begin
AdvRichEditor1.Font.Style := [fsBold];
AdvRichEditor1.Font.Color := clBlue;
AdvRichEditor1.AddText('Hello world,');
AdvRichEditor1.Font.Style := [fsItalic];
AdvRichEditor1.Font.Color := clRed;
AdvRichEditor1.AddText('this text is in red italic');
end;
Supported Delphi versions
The oldest version of Delphi that is supported for TAdvRichEditor is Delphi XE.
TAdvRichEditor internally uses generics which are not available in older versions of Delphi.
Sending TAdvRichEditor formatted text by email
Sending an email with the formatted text created by TAdvRichEditor is really simple. To do so, drop a TAdvRichEditor on the form and the Indy idSMTP component.
The content of the TAdvRichEditor can be sent with:
var
msg: TIdMessage;
Textpart: TidText;
begin
msg := TIdMessage.Create(self);
try
msg.ContentType := ''multipart/alternative'';
TextPart := TIdText.Create(msg.MessageParts);
TextPart.ContentType := ''text/plain'';
TextPart.Body.Clear;
TextPart.Body.Text := AdvRichEditor1.ContentAsPlainText;
TextPart := TIdText.Create(msg.MessageParts);
TextPart.ContentType := ''text/html'';
TextPart.Body.Clear;
TextPart.Body.Text := AdvRichEditor1.ContentAsHTML;
msg.From.Address := ''developer@delphi.com'';
msg.From.Text := ''Delphi Developer'';
msg.Recipients.Add.Address := ''joe.do@mailbox.com'';
msg.Subject := ''Your message subject here’;
IdSMTP1.Send(msg);
finally
msg.Free;
end;
end;
How to copy formatted text from a TAdvRichEditor to a TPlannerItem
To move the content of TAdvRichEditor with formatting to a selected PlannerItem, following code can be used:
begin
if Assigned(planner1.Items.Selected) then
begin
planner1.Items.Selected.Text.Text := advricheditor1.ContentAsRTF;
end;
end;
How to copy contents from one TAdvRichEditor instance to another
Copying contents from one TAdvRichEditor instance to another can be done in following way:
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
AdvRichEditor1.SaveToStream(ms);
ms.Position := 0;
AdvRichEditor2.LoadFromStream(ms);
finally
ms.Free;
end;
end;