Recent

Author Topic: Find Next in Memo - doesnt show word  (Read 171 times)

Raf20076

  • Full Member
  • ***
  • Posts: 138
    • https://github.com/Raf20076
Find Next in Memo - doesnt show word
« on: January 24, 2021, 11:12:48 am »

Hi Guys

I have Find Next procedure in Memo but it doesn't work correctly. It doesn't highlight word correctly when it found.
Also it doesn't highlight the last word if found. Something is wrong with position. I need UTF8 in this example. Have you got any ideas?

Thanks

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus,
  9.   ComCtrls,LazFileUtils, LCLProc, LazUtils, LazUtf8, Variants, strutils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     CheckButton: TButton;
  17.     Memo: TMemo;
  18.     procedure CheckButtonClick(Sender: TObject);
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   SearchAfterPos: Integer=0;
  29.  
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37.  
  38.  
  39. procedure TForm1.CheckButtonClick(Sender: TObject);
  40. //
  41. var
  42.   s: string;
  43.   i, searchstart: Integer;
  44.   Selected : String;
  45.  
  46.  
  47. begin
  48.  
  49.   searchstart:=Memo.SelStart+Memo.SelLength+1;
  50.   Selected := 'łabędź';//find this word
  51.   searchstart:=SearchAfterPos+1;
  52.   s := Copy(Memo.Text, searchstart, length(Memo.Text)-(searchstart));
  53.   i := PosEx(Selected, s);
  54.   if (i <> 0) then
  55.   begin
  56.  
  57.     Memo.SelStart := (i-2)+(searchstart);
  58.     Memo.SelLength := Length(Selected);
  59.     Memo.SetFocus;
  60.     SearchAfterPos:=SearchAfterPos+i+Length(Selected)-1;
  61.     end else begin
  62.     ShowMessage('The word is found no further.');
  63.  
  64.     SearchAfterPos:=0;
  65.  
  66.     end;
  67. end;
  68.  
  69. end.
  70.  
  71.  
  72.  

paweld

  • Sr. Member
  • ****
  • Posts: 262
Re: Find Next in Memo - doesnt show word
« Reply #1 on: January 24, 2021, 12:15:53 pm »
Code: Pascal  [Select][+][-]
  1. s := Copy(Memo1.Text, searchstart, length(Memo1.Text) - (searchstart) + 1);
Best regards
paweld

Raf20076

  • Full Member
  • ***
  • Posts: 138
    • https://github.com/Raf20076
Re: Find Next in Memo - doesnt show word
« Reply #2 on: January 24, 2021, 12:18:21 pm »
Thanks

I changed my code adding UTF8Length insted of Length, UTF8copy insted of Copy and and it seems to works now.

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus,
  9.   ComCtrls,LazFileUtils, LCLProc, LazUtils, LazUtf8, Variants, strutils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     CheckButton: TButton;
  17.     Memo: TMemo;
  18.     procedure CheckButtonClick(Sender: TObject);
  19.  
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   SearchAfterPos: Integer=0;
  29.  
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37.  
  38.  
  39. procedure TForm1.CheckButtonClick(Sender: TObject);
  40. //
  41. var
  42.   s: string;
  43.   i, searchstart: Integer;
  44.   Selected : String;
  45.  
  46.  
  47. begin
  48.  
  49.   searchstart:=Memo.SelStart+Memo.SelLength+1;
  50.   Selected := 'łabędź';
  51.   searchstart:=SearchAfterPos+1;
  52.   s := UTF8copy(Memo.Text, searchstart, length(Memo.Text)-(searchstart));//changed to UTF8
  53.   i := PosEx(Selected, s);
  54.   if (i <> 0) then
  55.   begin
  56.  
  57.     Memo.SelStart := (i-2)+(searchstart);
  58.     Memo.SelLength := UTF8Length(Selected);//changed to UTF8
  59.     Memo.SetFocus;
  60.     SearchAfterPos:=SearchAfterPos+i+UTF8Length(Selected)-1;//changed to UTF8
  61.     end else begin
  62.     ShowMessage('The word is found no further.');
  63.  
  64.     SearchAfterPos:=0;
  65.  
  66.     end;
  67. end;
  68.  
  69. end.
  70.  
  71.  
« Last Edit: January 24, 2021, 12:34:04 pm by Raf20076 »

 

TinyPortal © 2005-2018