Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TAdvRichEditor
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;