Recent

Author Topic: [SOLVED]RichMemo StringReplace?  (Read 3104 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: 742
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: 2786
    • 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: 2786
    • 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

 

TinyPortal © 2005-2018