Recent

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

laznewb

  • New Member
  • *
  • Posts: 20
[SOLVED]RichMemo StringReplace?
« on: February 16, 2020, 09:22:49 pm »
I'm trying to replace a string in RichMemo. I found the StringReplace function in the freepascal.org docs, but I'm struggling to implement it for some reason. Here is my code:

Code: Pascal  [Select][+][-]
  1. RichMemo1.Lines.Text := StringReplace(RichMemo1.Lines.Text, 'String to replace', 'Orange', [rfReplaceAll]);

I get no errors, it just doesn't do anything. Does anyone know what's wrong?
« Last Edit: February 18, 2020, 11:13:25 am by laznewb »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: RichMemo StringReplace?
« Reply #1 on: February 16, 2020, 09:38:25 pm »
Hi!

'String to replace' is not contained in your RichMemo1.Lines.Text.

Winni

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #2 on: February 16, 2020, 09:45:20 pm »
Hi!

'String to replace' is not contained in your RichMemo1.Lines.Text.

Winni
I double checked it and made sure that it was, and it was, but then I moved it to a different procedure (instead of FormCreate), and now it works. Do you know how I can keep the formatting of the other text in the RichMemo?
« Last Edit: February 16, 2020, 10:12:57 pm by laznewb »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: RichMemo StringReplace?
« Reply #3 on: February 16, 2020, 10:10:00 pm »
Hi!

FormCreate exists to iniialize your component - not to execute some cod that relies that everything is already initialized.

What do you want to do? More find and replace?

Put a Button on Form1 for find/replace.

Create a small second Form with one Edit for search and another for replace.
Put a Button on the second from with find/replace.

Then do your stringreplace.

Winni

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: RichMemo StringReplace?
« Reply #4 on: February 16, 2020, 10:59:24 pm »
Put a Button on Form1 for find/replace.

Create a small second Form with one Edit for search and another for replace.
Put a Button on the second from with find/replace.

Why are you re-inventing TReplaceDialog?

Bart

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: RichMemo StringReplace?
« Reply #5 on: February 16, 2020, 11:17:41 pm »
Hi!

Yes Bart, you are right .

But re-inventing makes a lot of fun ....

Winni

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #6 on: February 17, 2020, 12:02:30 am »
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.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: RichMemo StringReplace?
« Reply #7 on: February 17, 2020, 12:33:04 am »
Hi!

As I see you want realy to work with RichMemo.

As Bart said: We don't want to invent the wheel again.

Here are two pages with a lot of information:

wiki.freepascal.org/RichMemo#TRichMemo

https://wiki.freepascal.org/RichMemo/WorkArounds

Keep on hacking!

Winni

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: RichMemo StringReplace?
« Reply #8 on: February 17, 2020, 02:31:02 am »
So I have a question about the TReplaceDialog , since I've never used it. I just checked it and it gives you a option to search entire file ? How do you specify a file path\name ?

  I think that is a little misleading wouldn't you say ?
The only true wisdom is knowing you know nothing

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: RichMemo StringReplace?
« Reply #9 on: February 17, 2020, 03:38:52 am »
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.
StringReplace does what it says, it replaces the string. But it does not know anything about the formatting that RichMemo would like to apply to the text in the string.

You probably need to look what that formatting is, change your string and then apply that formatting to the newly inserted string. Gets messy of course if the string you are replacing is not all the same format.

Rich memo has functions for you to read the formatting and reapply it. Just be aware that not everything works on all platforms.

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 #10 on: February 17, 2020, 10:44:48 am »
So I have a question about the TReplaceDialog , since I've never used it. I just checked it and it gives you a option to search entire file ? How do you specify a file path\name ?

  I think that is a little misleading wouldn't you say ?

Not really: What that check usually means is whether you want to replace starting at the current caret position or from the beginning (or end, if backwards) of the text. Or, sometimes, search only in the selection versus the whole text. So yeah, maybe it's a little misleading ;)

Frankly, I like better how the Lazarus dialog is built: it says clearly "Origin: From cursor / From beginning" and has an option for "Scope: Selection / Global". Kudos to whoever designed it :)
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.

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #11 on: February 17, 2020, 06:31:30 pm »
Thank you for replies everyone. I think I found a solution, but I don't know how to implement it. See, what makes my problem possibly a lot simpler is that the RichMemo contents is read-only, it's contents do not change, except for the one string that I am trying to insert into the RichMemo - and the position this string is to be inserted at remains constant too.

I found the InsertStyledText procedure on the RichMemo wiki page:

Code: Pascal  [Select][+][-]
  1. procedure InsertStyledText(
  2.   const ARichMemo: TCustomRichMemo;
  3.   const TextUTF8: String;
  4.   AStyle: TFontStyles;
  5.   InsPos : Integer = -1 )

How do I implement this procedure? I tried this:

Code: Pascal  [Select][+][-]
  1. procedure InsertStyledText(
  2.   const ARichMemo: TCustomRichMemo;
  3.   const TextUTF8: String;
  4.   AStyle: TFontStyles;
  5.   InsPos : Integer = -1 );
  6. begin
  7. end;  
  8.  
  9. procedure TForm1.Button1(Sender: TObject);
  10. begin
  11.   InsertStyledText(RichMemo1, 'hello', [fsBold], 32);
  12. end;

This doesn't do anything though. But I want 'hello' the be inserted at position 32.

I know how to work with functions, but I have no idea what to do with this procedure - it's not part of RichMemo.

Thanks.
« Last Edit: February 17, 2020, 06:33:34 pm by laznewb »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: RichMemo StringReplace?
« Reply #12 on: February 17, 2020, 06:48:13 pm »
what os?

laznewb

  • New Member
  • *
  • Posts: 20
Re: RichMemo StringReplace?
« Reply #13 on: February 17, 2020, 07:21:29 pm »
what os?
I'm on Windows 10. Thanks for testing it on your end - did you do your procedure the same as me? In the background there, it looks like you haven't declared the InsertStyledText procedure, but it might just be out of view.
THANKS!

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: RichMemo StringReplace?
« Reply #14 on: February 17, 2020, 09:08:10 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;

 

TinyPortal © 2005-2018