Recent

Author Topic: TRichMemo - add line by line  (Read 12691 times)

mdalacu

  • Full Member
  • ***
  • Posts: 202
    • dmSimpleApps
TRichMemo - add line by line
« on: September 14, 2011, 12:42:47 pm »
Hi, i need a simple example or a hint regarding adding lines in a TRichMemo with different styles or colors.
I have tried everyting..it is such a simple task and i can't do it.. :'(
I need something like this (pseudocode):
 RichMemo.Lines.Clear;
 RichMemo1.SetTextAttributes(style1);
 RichMemo.Lines.('Sample text 1');
 RichMemo1.SetTextAttributes(style2);
 RichMemo.Lines.('Sample text 2');
 .. and so on...

Thank you!


Arbee

  • Full Member
  • ***
  • Posts: 223
Re: TRichMemo - add line by line
« Reply #1 on: September 14, 2011, 01:44:06 pm »
I have not used Richmemo very often, but this (in my eyes terribly dirty) coding works to get lines in different colors:

Code: [Select]
  with Richmemo1 do begin
     clear;
     SetRangeColor(0,1000,clRed);
     Append('Line in red');
     SelStart := 999999;
     SetRangeColor(SelStart,1000,clBlue);
     Append('Line in blue');
     SelStart := 999999;
     SetRangeColor(SelStart,1000,clGreen);
     Append('Line in green');
   end;
1.0/2.6.0  XP SP3 & OS X 10.6.8

mdalacu

  • Full Member
  • ***
  • Posts: 202
    • dmSimpleApps
Re: TRichMemo - add line by line with different colors
« Reply #2 on: September 14, 2011, 03:45:13 pm »
Thank you for your replay, it works. :D
 I have updated the example a bit, it's a little bit faster:
Code: [Select]
    with RichMemo1 do
      begin
        Clear;
        Lines.Add('Line in red');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clRed);

        Lines.Add('Line in black ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clBlack);

        Lines.Add('Line in blue ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clBlue);

        Lines.Add('Line in green ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clGreen);

        Lines.Add('Line in yellow ');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clYellow);

        Lines.Add('Normal line.');
        Lines.Add('Normal line.');
        Lines.Add('Line in gray');
        SetRangeColor(Length(RichMemo1.Lines.Text)-Length(RichMemo1.Lines[RichMemo1.Lines.Count-1])-RichMemo1.Lines.Count-1,Length(RichMemo1.Lines[RichMemo1.Lines.Count-1]),clGray);
      end;
« Last Edit: September 14, 2011, 03:47:01 pm by mdalacu »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TRichMemo - add line by line
« Reply #3 on: September 15, 2011, 11:14:17 am »
Just a sidenote, you have the whole code in "with RichMemo1 do", so you don't need to call RichMemo1 inside it  :)

If you only need to color 10 or less lines with the code its fast enough, but overuse of length() should be avoided. You can use variables to save last index position.

mdalacu

  • Full Member
  • ***
  • Posts: 202
    • dmSimpleApps
Re: TRichMemo - add line by line
« Reply #4 on: June 19, 2012, 12:12:17 pm »
you are right, here is the updated example:
Code: [Select]
with RichMemo1 do
      begin
        Clear;
        Lines.Add('Line in red');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);

        Lines.Add('Line in black ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clBlack);

        Lines.Add('Line in blue ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clBlue);

        Lines.Add('Line in green ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clGreen);

        Lines.Add('Line in yellow ');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clYellow);

        Lines.Add('Normal line.');
        Lines.Add('Normal line.');
        Lines.Add('Line in gray');
        SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clGray);
      end;             

For Linux you have to modify the line like this:
Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-1,Length(Lines[Lines.Count-1]),clYellow);And also for the component to compile under Ubuntu you have to replace gtk2richmemo with the one attached.

Anyway, Lazarus really needs an updated component capable of displaying formatted text.
« Last Edit: June 19, 2012, 01:52:28 pm by mdalacu »

evoshroom

  • Full Member
  • ***
  • Posts: 157
Re: TRichMemo - add line by line
« Reply #5 on: July 08, 2012, 11:24:17 pm »
Quote
Anyway, Lazarus really needs an updated component capable of displaying formatted text.

I wholeheartedly agree.

I've found Richmemo to be great on Mac, but on Windows is where it seems lacking and it needs a way to add links and pics.

Is there a way to not count carriage returns as characters on Windows?  It seems the default behavior is the carriage returns don't count on Mac but do count on Windows, requiring separate $IFDEF'ed codebases.

Also, have you found any way to add line-by-line on Windows using more than one color per line and where it doesn't first add the line in one color and then change it (this looks horrible for users, but only seems to affect Windows)?

Lagavulin16

  • New member
  • *
  • Posts: 5
Re: TRichMemo - add line by line
« Reply #6 on: December 03, 2012, 04:26:44 pm »
From mdalacu's sample:

Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);
Excuse my ignorance but what is this "-Lines.Count-1" for?

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: TRichMemo - add line by line
« Reply #7 on: December 07, 2012, 09:53:50 pm »
From mdalacu's sample:

Code: [Select]
SetRangeColor(Length(Lines.Text)-Length(Lines[Lines.Count-1])-Lines.Count-1,Length(Lines[Lines.Count-1]),clRed);
Excuse my ignorance but what is this "-Lines.Count-1" for?
assumption warning

the number of LF chars?
its heavily OS depended since in windows this should multiplied by 2
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

p95x

  • New member
  • *
  • Posts: 5
  • Hi.
Re: TRichMemo - add line by line
« Reply #8 on: January 10, 2013, 07:42:48 pm »
Hello,

as a solution for your issue, you can look at my procedure I have written and added to RichMemo.pas

Here, I have posted the functions:
http://www.lazarus.freepascal.org/index.php/topic,19446.0.html

Lazarus IDE v1.0.4, FPC 2.6.0 x86_64 ; Windows 8