InsertText in different color of the text in richview

General TRichView support forum. Please post your questions here
Post Reply
sergio_morisue
Posts: 13
Joined: Tue Feb 06, 2007 2:48 pm
Location: Cambui, Brazil

InsertText in different color of the text in richview

Post 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.
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
sergio_morisue
Posts: 13
Joined: Tue Feb 06, 2007 2:48 pm
Location: Cambui, Brazil

InsertColorText

Post 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?
Sergey Tkachenko
Site Admin
Posts: 17565
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Can you create a simple project reproducing the problem and send it to me?
sergio_morisue
Posts: 13
Joined: Tue Feb 06, 2007 2:48 pm
Location: Cambui, Brazil

I think that found the problem

Post 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.
Post Reply