### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [solved] RichMemo Searching the quickest way for writing - some digits BOLD  (Read 632 times)

#### Nicole

• Hero Member
• Posts: 743
##### [solved] RichMemo Searching the quickest way for writing - some digits BOLD
« on: January 31, 2023, 06:03:35 pm »
There is a text, where some digits shall be written in bold.

You say, this is no problem?
I agree, if you have just a text and some lines.

In Delphi and RichEdit, I could just set the start of the bold text and then the end.
The select of RichMemo seems to be much more complex.
Or is there an easy way?

This is the method, which generate the content of the Richmemo.
Three figures (not lines) of it shall be bold.

I post the whole text-generation process for you to understand.
No, you need not read this. It is just to illustrate my needs.

It would be great just to fill into the string /hb ............ /he for highlight begin and highlight end.
The language which offered me that, - was not fp.
I would be graete to have somer suggestions, how to make it with a minimum of complexity and maintenance in the future.

Code: Pascal  [Select][+][-]
2.
3.    if Abs(EinTrade_.Pos_) > 1 then begin
4.     if StaticText_invisible.Caption <> '' then   // Leerabfrage deshalb, weil ich uU einen Mehrpositionentrade schon initial importiere
5.       RichEdit_Tradeausgabe.Lines.Add(StaticText_invisible.Caption);   // da hätte ich sonst einen Zeilenvorschub durch die Leerzeilen,
6.     if StaticText_invisbile_Stop1Kontrakt.Caption <> '' then     // weil eben die Positonserhöhung nicht erst importiert wird
8.     if StaticText_invisbile_target1Kontrakt.Caption <> '' then
10.    end;
11.    s_Ausgabe:='';
12.    s_Ausgabe:=s_Ausgabe + IntToStr(EinTrade_.Pos_) + ' ' ;
13.
14.    if EinTrade_.Pos_ > 0
15.        then  s_Ausgabe:=s_Ausgabe + ' long '
16.        else  s_Ausgabe:=s_Ausgabe + ' short ';
17.    s_Ausgabe:=s_Ausgabe + EinTrade_.Kontraktname;
18.    RichEdit_Tradeausgabe.Lines.Add(s_Ausgabe); s_Ausgabe:='';   // wird übertragen und neuer Block begonnen
19.
20.    s_Ausgabe:=s_Ausgabe + ' Entry: ' + FloatToStr (EinTrade_.EntryPrice);
21.    s_Ausgabe:=s_Ausgabe + ' (' + DateToStr (EinTrade_.EntryDate);
22.    if EinTrade_.EntryTime <> 0 then     //    result:=FormatDateTime('" "dddd" am "dd"."mmmm" "yyyy," "hh":"nn" ""Uhr',dat);  FormatDateTime('hh:nn', DeinedateTime)
23.        s_Ausgabe:=s_Ausgabe + ' um ' + FormatDateTime('hh:nn', EinTrade_.EntryTime); // TimeToStr (EinTrade_.EntryTime, '"hh":"nn"'); wäre mit Sekunden
24.    s_Ausgabe:=s_Ausgabe + ')';
25.
26.    RichEdit_Tradeausgabe.Lines.Add(s_Ausgabe); s_Ausgabe:='';   // wird übertragen und neuer Block begonnen
27.
28.    s_Ausgabe:=s_Ausgabe + ' Stop: ' + FloatToStr (EinTrade_.stop);   // in Klammer Risiko
29.    s_Ausgabe:=s_Ausgabe + ' (\$ ' + FloatToStr (EinTrade_.valueAtRisk) + ')';
31.    StaticText_invisbile_Stop1Kontrakt.Caption:='(pro Kontrakt ' + s_Ausgabe+ ')'; // gesichert für Mehr-Kontraktausgabe
32.    s_Ausgabe:='';   // wird übertragen und neuer Block begonnen
33.
34.    s_Ausgabe:=s_Ausgabe + ' tar: ' + FloatToStr (EinTrade_.target);  // in Klammer Chance und PL
35.    s_Ausgabe:=s_Ausgabe + ' (\$ ' + FloatToStr (EinTrade_.expectedProfit);
36.    StaticText_invisbile_target1Kontrakt.Caption:='(pro Kontrakt ' + s_Ausgabe+ ')';
37.    s_Ausgabe:=s_Ausgabe + ', PL=' + FloatToStrF (EinTrade_.PLR,ffNumber,5,2) + ') '+ #13#10;
38.    s_Ausgabe:=s_Ausgabe + ' Exit: ' + FloatToStr (EinTrade_.ExitPrice);
39.    s_Ausgabe:=s_Ausgabe + ' (' + DateToStr (EinTrade_.ExitDate);
40.    if EinTrade_.ExitTime <> 0 then
41.         s_Ausgabe:=s_Ausgabe + ' um ' + FormatDateTime('hh:nn', EinTrade_.ExitTime);  // TimeToStr (EinTrade_.ExitTime); wäre mit Sekunden
42.    s_Ausgabe:=s_Ausgabe + ')';
43.
46.
47.    if (EinTrade_.daily_limit  <> 0) then
49.
50.    s_Ausgabe:='';
51.    if EinTrade_.Ergebnis <> 0 then // das ist blöd, weil mir das Null Ergebnis dann nicht angezeigt wird
52.       begin
53.        if EinTrade_.Ergebnis >= 0 then s_Ausgabe:=s_Ausgabe + ' Gewinn: '
54.                                   else s_Ausgabe:=s_Ausgabe + ' Verlust:  ';
55.        s_Ausgabe:=s_Ausgabe + FloatToStr (EinTrade_.Ergebnis)
56.       end;

« Last Edit: February 01, 2023, 08:15:16 pm by Nicole »

#### KodeZwerg

• Hero Member
• Posts: 1210
• Fifty shades of code.
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #1 on: January 31, 2023, 06:34:23 pm »
First thing that comes into my mind is that you create a method in different variants (overloaded) to accept your needed input types and in your current method just call it to produce a line of text, ready formatted for RE.
And a small seperate method to add linebreaks.
That you just call your custom lineformatter with your given inputs, put linebreaks etc until the end.

Does that make any sense to you?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

#### rvk

• Hero Member
• Posts: 5143
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #2 on: January 31, 2023, 07:09:05 pm »
In Delphi and RichEdit, I could just set the start of the bold text and then the end.
The select of RichMemo seems to be much more complex.
Or is there an easy way?
You mention Delphi and TRichEdit. Isn't it in TRichMemo the same way??

From https://wiki.freepascal.org/RichMemo
Code: Pascal  [Select][+][-]
1. RichMemo1.SetRangeParams (
2.      RichMemo1.SelStart, RichMemo1.SelLength,
3.      [tmm_Styles, tmm_Color], // changing Color and Styles only
4.      '',  // this is font name - it's not used, thus we can leave it empty
5.      0,  // this is font size - it's font size, we can leave it empty
6.      clGreen, // making all the text in the selected region green color
7.      [fsBold, fsItalic],  // adding Bold and Italic Styles
8.      []
9.   );

And you are still using TRichMemo.Lines.Add().
That's only for adding UNFORMATTED lines.

Also from https://wiki.freepascal.org/RichMemo
Quote
Code: Pascal  [Select][+][-]
1. procedure InsertFontText(
2.   const ARichMemo: TCustomRichMemo;
3.   const TextUTF8: String;
4.   const prms: TFontParams;
5.   InsPos : Integer = -1 )
InsertFontText inserts the text and applies specified FontParams to it.

You might want to create a class helper to implement these functions as methods for RichMemo. Beware, if you're using Delphi-compatibility helper - you might right into a conflict.

So, if you want to add formatted text you should use TRichMemo.InsertFontText.
Of TRichMemo.Lines.Add and apply RichMemo1.SetRangeParams afterwards (which is more difficult because you need to scan for the Start and Length of your text).

#### Nicole

• Hero Member
• Posts: 743
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #3 on: January 31, 2023, 07:53:35 pm »

Unfortunately Delphi's RichEdit and fp's RichMemo differ very much in points of formatting.
In Delphi I had a solution for my "bold", which worked fine. But it did not work with RichMemo.
Believe me, re-use was the first, which I tried.

Delphi is left behind and I will never go back to it.
So I do not need any compatible code.

This

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

looks great to me.

How to use it?

e.g. I say "InsertFontText(myRichMemo, 'myBoldAttempt' , ...... aeh 1 ...., aeh 2 .....);

aeh 1
Font Params shall be an array, shall it? Like [...., bold...] ?
Do you have an example how to write the parameters correctly?

aeh 2
How to get the insPos?

and how to stop the bold part?
Just by calling the method again, with a "normal" (or however not-bold may be called) ?

Thank you for your help.

#### rvk

• Hero Member
• Posts: 5143
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #4 on: January 31, 2023, 07:58:09 pm »
Here is an example.

Code: Pascal  [Select][+][-]
1. uses ... RichUtilsUtils ..
2. ..
3. var
4.   prms : TFontParams;
5. begin
6.   prms := GetFontParams(RichMemo1.Font);
7.   prms.Color := clBlue;
8.   prms.Style := [fsBold];
9.   // this adds the blue and bolded Hello World at the end of the richmemo
10.   // it's possible to pass the 4th parameter to the function to specify
11.   // where the text should be added. By default - at the end.
12.   InsertFontText(RichMemo1,'hello world', prms);
13. end;

#### Nicole

• Hero Member
• Posts: 743
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #5 on: February 01, 2023, 07:00:29 pm »
This method is part of my code now, but I fail at

and later by
InsertFontText()

Probably I must take RichUtilsUtils somehow from the unit RichUtils, but although I was told before how to do this, - I do not know it any more.

#### rvk

• Hero Member
• Posts: 5143
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #6 on: February 01, 2023, 07:51:03 pm »
This method is part of my code now, but I fail at
Looking at it, I think that's a typo on the Wiki.

It should be just RichMemoUtils

#### Nicole

• Hero Member
• Posts: 743
##### Re: RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #7 on: February 01, 2023, 08:14:55 pm »
This is it!
I though about a typo, but tried the wrong ones.

This works not, thank you so much.

#### Nicole

• Hero Member
• Posts: 743
##### Re: [solved] RichMemo Searching the quickest way for writing - some digits BOLD
« Reply #8 on: February 02, 2023, 07:42:56 pm »
Here is a working solution for putting a part of a line in a RichMemo into bold

call it by

Code: Pascal  [Select][+][-]
1.  BoldText(myRichEdit,'I start the line' , ' I am bold');

Code: Pascal  [Select][+][-]
1.  uses RichMemoUtils

Code: Pascal  [Select][+][-]
1. procedure BoldText(RichMemo_: TRichMemo; zeilenanfang, text_: string);
2.
3.  var
4.   prms, prms_backup : TFontParams;
5.
6.
7. begin
8.   prms := GetFontParams(RichMemo_.Font);
9.   prms_backup:= GetFontParams(RichMemo_.Font);  // saves the style of the RichMemo
10. //  prms.Color := clBlue;  // a color may be set
11.   prms.Style := [fsBold];
12.   // this adds the blue and bolded
13.   // it's possible to pass the 4th parameter to the function to specify
14.
15.   InsertFontText(RichMemo_, zeilenanfang, prms_backup);  // starts the line in default style
16.
17.   InsertFontText(RichMemo_, text_, prms);  // writes on the line bold or colored
18.
19.   InsertFontText(RichMemo_, '.. ', prms_backup);  // sets the style back
20. end;