Recent

Author Topic: [SOLVED]RichMemo StringReplace?  (Read 3619 times)

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #15 on: February 17, 2020, 10:21:46 pm »
it looks like you haven't declared the InsertStyledText procedure, but it might just be out of view.
you don't need to declare the procedure. It's declared and implemented in richmemoutils.
all you need is to add the unit to the uses section
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, ..., Dialogs, RichMemo, RichMemoUtils;
Thank you very much, I missed out the RichMemoUtils unit - it's working perfectly now.

Hi!

FormCreate exists to iniialize your component - not to execute some cod that relies that everything is already initialized.
I've always used FormCreate to execute code in pretty much the same manner before, this is the first time it has not worked - what other procedure can I use to execute code as soon as the form (and it's components) have initialized?

Thanks all.

Hartmut

  • Hero Member
  • *****
  • Posts: 756
Re: RichMemo StringReplace?
« Reply #16 on: February 18, 2020, 09:56:31 am »
I've always used FormCreate to execute code in pretty much the same manner before, this is the first time it has not worked - what other procedure can I use to execute code as soon as the form (and it's components) have initialized?

You can use FormActivate() for that. Look in https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tcustomform.onactivate.html
Quote
This handler is called when the form receives focus for the first time at application start up and then subsequently each time focus is changed from another window of the same application to this window.
For focus changes between different applications the Application.OnActivate handler is called instead.
So be carefull, that FormActivate() can be called more then once. If I have code there, that should be executed only once, I use a local typed const for that:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);  
  2.    const first: boolean = true;
  3.    begin
  4.    if first then
  5.       begin
  6.       ...
  7.       first:=false;
  8.       end;
  9.  
  10.    ...
  11.    end;
  12.  

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #17 on: February 18, 2020, 11:13:12 am »
Thank you, I did see that on the wiki, but I didn't know how to only call something once only - thanks very much for the example.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: RichMemo StringReplace?
« Reply #18 on: February 18, 2020, 01:45:19 pm »
Thank you, I did see that on the wiki, but I didn't know how to only call something once only [...]

If everything in the OnActivate handler is only to execute just once this is a (IMHO) better way:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);  
  2. begin
  3.   OnActivate := Nil; {So it's never called again}
  4.   { ... do whatever else ... }
  5. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: RichMemo StringReplace?
« Reply #19 on: February 18, 2020, 11:38:09 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);  
  2. begin
  3.   OnActivate := Nil; {So it's never called again}
  4.   { ... do whatever else ... }
  5. end;
Wow, neat trick !

What happened to the wiki page we talked about listing clever tricks like this ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: RichMemo StringReplace?
« Reply #20 on: February 19, 2020, 12:57:23 am »
Wow, neat trick !

 :D

Here is another one. There is a little gem in TCustomForm that allows you to set a handler for the first, an only the first OnShow event fired. It's used like this:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. type
  4.   TMainForm = class(TForm)
  5.     {... normal declarations ...}
  6.   public
  7.     procedure FormCreate(Sender: TObject);
  8.     procedure FirstShow(Sender: TObject);
  9.   end;
  10.  
  11. {... etc ...}
  12.  
  13. implementation
  14.  
  15. procedure TMainForm.FormCreate(Sender: TObject);
  16. begin
  17.   AddHandlerFirstShow(FirstShow, True);
  18.   {There is a corresponding RemoveHandlerFirstShow}
  19. end;
  20.  
  21. procedure TMainForm.FirstShow(Sender: TObject);
  22. begin
  23.   {Do whatever the very first time the form is shown and only then.}
  24. end;

Seems as if there should be a related OnFirstShow event but for some reason it isn't there; this technique allows you to mimic its working.

What happened to the wiki page we talked about listing clever tricks like this ?

Dunno. I don't remember having ever talked about it. :-[
Sound like a good idea though it would need quite some maintenance.
« Last Edit: February 19, 2020, 01:00:33 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: RichMemo StringReplace?
« Reply #21 on: February 19, 2020, 11:30:12 pm »
What happened to the wiki page we talked about listing clever tricks like this ?

Dunno. I don't remember having ever talked about it. :-[
Sound like a good idea though it would need quite some maintenance.

It was Handoko's idea, I was using the "collective we" !
https://forum.lazarus.freepascal.org/index.php/topic,48264.msg347466.html#msg347466

But great example !
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

ReinaldoDuvida

  • Newbie
  • Posts: 6
Re: RichMemo StringReplace?
« Reply #22 on: April 22, 2024, 08:24:18 pm »
What do you want to do? More find and replace?
The StringReplace works now, but it removes all existing Rtf formatting in the RichMemo. I was wondering if there is a way to preserve this formatting, and Replace the string with the same font as the string that is being replaced? Thanks.

I discovered a way to use StringReplace without losing RichMemo's previous formatting. First I convert the scanned text with its formatting to a StringStream and place this stream value in a Memo. Then I will use StringReplace on the Memo text. Then I converted the memo text to a StringStream and made RichMemo read this StringStream.

Converting RichMemo text to a StringStream and placing the stream value in the Memo:
Code: Pascal  [Select][+][-]
  1. procedure TfRichTextDois.GetStream;
  2. var
  3.   stStringStream : TStringStream;
  4. begin
  5.   Memo1.Clear;
  6.   stStringStream := TStringStream.Create;
  7.   RichMemo1.SaveRichText(stStringStream);
  8.   Memo1.Lines.Add(stStringStream.DataString);
  9.   FreeAndNil(stStringStream);
  10. end;
  11.  

Using StringReplace in Memo:
Code: Pascal  [Select][+][-]
  1. RichMemo1.Lines.Text := StringReplace(RichMemo1.Lines.Text,
  2.                                                           edt_wordsearch.Text,
  3.                                                           edt_wordreplace.Text,
  4.                                                           [rfReplaceAll]);  
  5.  

Taking the memo value, converting it to a StringStream and making RichMemo read this stream:
Code: Pascal  [Select][+][-]
  1. procedure TfRichTextDois.SetStream;
  2. var
  3.   stStringStream : TStringStream;
  4. begin
  5.   stStringStream := TStringStream.Create;
  6.   stStringStream.WriteString(Memo1.Lines.Text);
  7.   stStringStream.Position := 0;
  8.   RichMemo1.LoadRichText(stStringStream);
  9.   FreeAndNil(stStringStream);
  10. end;    
  11.  


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2217
  • Fifty shades of code.
    • Delphi & FreePascal
Re: [SOLVED]RichMemo StringReplace?
« Reply #23 on: April 22, 2024, 08:44:40 pm »
As an alternate, nobody has thought about, simply use the RichMemo api to solve that problem like I have shown here.
Since I was in hurry while posting it, it might need adjustments to properly work.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018