Recent

Author Topic: [SOLVED] Highlight a particular line of a memo  (Read 1321 times)

w click

  • Full Member
  • ***
  • Posts: 180
[SOLVED] Highlight a particular line of a memo
« on: June 18, 2022, 06:34:54 pm »
How would you highlight, say, the fifth line of a memo without assigning selstart using pos or utf8pos.
 
Code: Pascal  [Select][+][-]
  1. s:=memo1.lines[5];
  2. Memo1.selstart:=UTF8Pos(s,Memo1.text)-1;
  3. memotext.sellength:=length(s);
  4. Memotext.Perform(EM_SCROLLCARET, 0, 0);
  5. Memotext.SetFocus;
Works fine, except when line 2 is the same as line 5 or has line 5 as a sub-string, then it highlights line 2.
I just want a select line x.
« Last Edit: June 19, 2022, 03:21:36 pm by w click »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Highlight a particular line of a memo
« Reply #1 on: June 18, 2022, 08:29:06 pm »
Hi!

The following solution works only if wordwrap is set to false.  Otherwise the numbers of lines might not be the same like the index of memo.lines

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var count,i, neededLine : integer;
  3. begin
  4. count := 0;
  5. neededLine := 3; // zero based
  6.  
  7. for i := 0 to MEMO1.Lines.Count - 1 do
  8.   begin
  9.   if i = neededLine then
  10.     begin
  11.     Memo1.SelStart:= count;
  12.     Memo1.SelLength:=   length(Memo1.lines[i]);
  13.     exit;
  14.     end;
  15.   count := count + length(Memo1.lines[i])+length(LineEnding);
  16.   end;
  17. end;                    

Winni

dje

  • Full Member
  • ***
  • Posts: 134
Re: Highlight a particular line of a memo
« Reply #2 on: June 19, 2022, 01:49:19 am »
Use CaretPos to move to a line? This works for me under Linux.

Code: Pascal  [Select][+][-]
  1.   AMemo.CaretPos := Point(0, ALine);
  2.   AMemo.SelLength := AMemo.Lines[ALine].Length;  

* Obviously add in bound error checking

w click

  • Full Member
  • ***
  • Posts: 180
Re: Highlight a particular line of a memo
« Reply #3 on: June 19, 2022, 01:21:15 pm »
Derek, I can't get 'Point' to work at all.

Winni, good idea, though I modified it a little.
Code: Pascal  [Select][+][-]
  1. s:=Memo1.lines[target];
  2. label6.caption:=inttostr(target)+'. <'+s+'> len='+inttostr(length(s))+' utf8len='+inttostr(utf8length(s));
  3.  
  4. if (target>-1)and(target<=Memo1.lines.count-1) then begin
  5.   count:=0; i:=0;
  6.   while i<target do begin
  7.     count:=count+length(Memo1.lines[i]);
  8.     i:=i+1;
  9.   end;
  10.   Memo1.selstart:=count;
  11.   Memo1.SelLength:=length(Memo1.lines[target]);
  12.   Memo1.setfocus;
  13. end;
  14.  
But it veers off, because the length isn't giving the right result.  I tried utf8length, but again an error creeps in.  Is it something to do with smart quotes (and other characters)?

rick2691

  • Sr. Member
  • ****
  • Posts: 444
Re: Highlight a particular line of a memo
« Reply #4 on: June 19, 2022, 01:38:34 pm »
w click,

I assume that you are using a utf8 font, since you tried utf8length. But afterward you are still doing ascii lengths ... length(Memo1.lines. Your length must always be utf8length, because utf8 uses multiple characters for one character.

Rick
Windows 11, LAZ 2.0.10, FPC 3.2.0, SVN 63526, i386-win32-win32/win64, using windows unit

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Highlight a particular line of a memo
« Reply #5 on: June 19, 2022, 02:12:15 pm »
Hi!

Utf8ength must be used, that is correct.

But you forget to add the

length(lineEnding)


Dont add just one ore two because Linux and Windows have a different length of the LineEnding

Winni

w click

  • Full Member
  • ***
  • Posts: 180
Re: Highlight a particular line of a memo
« Reply #6 on: June 19, 2022, 03:21:09 pm »
Thank you, much appreciated.  This worked.
Code: Pascal  [Select][+][-]
  1.  
  2.     if (target>-1)and(target<=Memo1.lines.count-1) then begin
  3.       count:=0; i:=0;
  4.       while i<target do begin
  5.         count:=count+utf8length(Memo1.lines[i])+utf8length(lineending);
  6.         i:=i+1;
  7.       end;
  8.       Memo1.selstart:=count;
  9.       Memo1.SelLength:=utf8length(Memo1.lines[target]);
  10.       Memo1.setfocus;
  11.     end;
  12.  

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Highlight a particular line of a memo
« Reply #7 on: June 20, 2022, 04:15:41 pm »
Thank you, much appreciated.  This worked.
In first topic you say "without assigning selstart".
Here is an example. It only reads the SelStart property. And no loops and UTF 8 function calls:
Code: Pascal  [Select][+][-]
  1. procedure SelectLineInMemo(Memo: TCustomMemo; LineIndex: SizeInt);
  2. var
  3.   SelEnd: SizeInt;
  4. begin
  5.   Memo.CaretPos := Point(0, LineIndex + 1); // It also works for last LineIndex
  6.   SelEnd := Memo.SelStart;
  7.   Memo.CaretPos := Point(0, LineIndex);
  8.   Memo.SelLength := SelEnd - Memo.SelStart;
  9. end;

w click

  • Full Member
  • ***
  • Posts: 180
Re: [SOLVED] Highlight a particular line of a memo
« Reply #8 on: June 26, 2022, 04:42:16 pm »
I couldn't get point to work, I'm afraid.

speter

  • Sr. Member
  • ****
  • Posts: 345
Re: [SOLVED] Highlight a particular line of a memo
« Reply #9 on: June 27, 2022, 02:43:09 am »
I couldn't get point to work, I'm afraid.

You may need to add "types" or ""classes" to your unit "uses" line.

cheers
S.
Code: Pascal  [Select][+][-]
  1. unit something;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms;
  9. // ^^^^
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

w click

  • Full Member
  • ***
  • Posts: 180
Re: [SOLVED] Highlight a particular line of a memo
« Reply #10 on: June 27, 2022, 06:16:31 pm »
Thanks for the help.  I had classes and I tried 'types' to no avail.  However, I've got something that works, so I've moved on to be stuck elsewhere in this project.

 

TinyPortal © 2005-2018