Page 1 of 1

InsertText in different color of the text in richview

Posted: Wed Feb 07, 2007 8:19 pm
by sergio_morisue
Sorry my english!
I use InsertText for insert one word in RichViewText, I need insert in different color in text. How i make this?
Thanks.
Please is possible one example? I'm beginner user.

Posted: Thu Feb 08, 2007 9:03 am
by Sergey Tkachenko

Code: Select all

function GetTextStyleWithColor(rve: TCustomRichViewEdit; Color: TColor): Integer;
var BackColor: TColor;
begin
  BackColor := rve.Style.TextStyles[rve.CurTextStyleNo].BackColor; 
  Result := rve.Style.TextStyles.FindStyleWithColor(rve.CurTextStyleNo,
    Color, BackColor);
  if Result<0 then begin
    // the style is not found, creating it
    rve.Style.TextStyles.Add;
    Result := rve.Style.TextStyles.Count-1;
    rve.Style.TextStyles[Result].Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
    rve.Style.TextStyles[Result].Color := Color;
    rve.Style.TextStyles[Result].Standard := False; 
  end; 
end;

procedure InsertColoredText(rve: TCustomRichViewEdit; const Text: String; Color: TColor);
var StyleNo: Integer;
begin
  StyleNo := rve.CurTextStyleNo;
  rve.CurTextStyleNo := GetTextStyleWithColor(rve, Color);
  rve.InsertText(Text);
  rve.CurTextStyleNo := StyleNo;
end;


The code above uses FindStyleWithColor function. It allows finding text style with the given text and background color. The most universal style searching function is FindSuchStyle. Search "FindSuchStyle" in this forum for examples.

InsertColorText

Posted: Thu Feb 08, 2007 2:47 pm
by sergio_morisue
Thanks very much!
I test the function in RichViewEdit, is perfect, insert into cursor position and search and replace, the function is all right.
But, using in DBRichViewEdit, a error ocurred: 'List index out of bounds', for DBRichViewEdit the function is different?

Posted: Thu Feb 08, 2007 4:03 pm
by Sergey Tkachenko
Can you create a simple project reproducing the problem and send it to me?

I think that found the problem

Posted: Thu Feb 08, 2007 11:16 pm
by sergio_morisue
I'm using DBRichView and DBRichViewEdit in form with same RVStyle for both. This cause an error.
With one RVStyle for each, the function run correctly.
The manner to use DBRichView, DBRichViewEdit, RichView and RichViewEdit is with one RVStyle for each? This is correct?
Thanks, for help.