Recent

Author Topic: RichMemo.Color behaviour at Runtime  (Read 2946 times)

AgriMensor

  • Newbie
  • Posts: 4
RichMemo.Color behaviour at Runtime
« on: January 27, 2022, 06:38:47 pm »
Hi!
I'm new to Pascal and Lazarus but have 25+ years of programming experience in other languages. Can anyone please help me with this problem?
I'm having difficulty changing the .Color at runtime in RichMemo. If I set it in the Object Inspector it works fine (on Windows 10 at least) but if I do something like the following, it changes the .color to black, not to the color selected from the ColorDialog. However, it does work fine for the ColorButton:

procedure TForm1.ColorButton2Click(Sender: TObject);
var
  Fred : TColor;
begin
  Fred := ColorDialog1.Color;
  ColorButton2.ButtonColor := Fred; //Works OK
  RichMemo1.Color := Fred; //Sets to Black
end;

Any helpful suggestions much appreciated!
Thanks,
Steve

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2731
    • havefunsoft.com
Re: RichMemo.Color behaviour at Runtime
« Reply #1 on: January 27, 2022, 07:46:19 pm »
what an interesting bug.

try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ColorButton2Click(Sender: TObject);
  2. var
  3.   Fred : TColor;
  4. begin
  5.   if not ColorDialog1.Execute then Exit;
  6.   Fred := ColorDialog1.Color;
  7.   ColorButton2.ButtonColor := Fred; //Works OK
  8.   RichMemo1.Color := Fred; //Sets to Black
  9. end;
« Last Edit: January 27, 2022, 07:48:13 pm by skalogryz »

AgriMensor

  • Newbie
  • Posts: 4
Re: RichMemo.Color behaviour at Runtime
« Reply #2 on: January 28, 2022, 12:27:20 pm »
Many thanks, skalogryz, this has cured the problem, but begs the question 'Why?'

Interestingly, I did originally try something similar:

Code: Pascal  [Select][+][-]
  1. if ColorDialog1.Execute then
  2.    begin
  3.       Fred := ColorDialog1.Color;
  4.       ColorButton2.ButtonColor := Fred;
  5.       RichMemo1.Color := Fred;
  6.    end;

with the same result as your code, but because both solutions make the ColorDialog appear twice (unless the Cancel button is pressed), I dropped the conditional clause.

So, it looks like that it's a case of the lesser of two evils :-(

Can this be reported as a bug and, if so, how?

Again, many thanks!

Steve

[Edited to add code tags; please read How to use the Forum].
« Last Edit: January 28, 2022, 12:40:12 pm by trev »

AlexTP

  • Hero Member
  • *****
  • Posts: 1765
    • UVviewsoft
Re: RichMemo.Color behaviour at Runtime
« Reply #3 on: January 28, 2022, 01:13:40 pm »
Before reporting, pls make tiny compilable example. (Attach in zip). I don't see that TColorDialog appears twice. Maybe you called .Execute twice.

AgriMensor

  • Newbie
  • Posts: 4
Re: RichMemo.Color behaviour at Runtime
« Reply #4 on: January 28, 2022, 03:14:39 pm »
Thanks Alextp!

I'm just testing RichMemo's capabilities and any potential flaws at the moment, so the relevant code is limited to that simple code already provided.

As far as I'm aware, I'm not calling ColorDialog twice. If you run the above code with just a ColorButton and a RichMemo on the form, you'll see that the RichMemo colour now changes just fine (as does the ColorButton) when you press the OK button on the ColorDialog, but the ColorDialog remains on screen (I believe it's actually a second instance) unless you press one of its buttons again. Interestingly, picking a different colour doesn't change the RichMemo colour again!

Steve

AlexTP

  • Hero Member
  • *****
  • Posts: 1765
    • UVviewsoft
Re: RichMemo.Color behaviour at Runtime
« Reply #5 on: January 28, 2022, 05:05:31 pm »
I cannot understand what happens on your side. maybe small project (in zip) + video can help.
(Linux program Kazam allows to record video)

AgriMensor

  • Newbie
  • Posts: 4
Re: RichMemo.Color behaviour at Runtime
« Reply #6 on: January 30, 2022, 06:46:49 pm »
Hi Alextp!
I only have access to Windows 10 and 7. I will try the .exe on 7, but can't easily do so under Linux.
Thanks for your help anyway!
Steve

paweld

  • Sr. Member
  • ****
  • Posts: 368
Re: RichMemo.Color behaviour at Runtime
« Reply #7 on: January 31, 2022, 10:47:00 am »
if you use TColorDialog, use the OnColorChanged event instead of OnClick, e.g.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ColorButton2ColorChanged(Sender: TObject);
  2. var
  3.   Fred : TColor;
  4. begin
  5.   Fred := ColorButton2.ButtonColor;
  6.   RichMemo1.Color := Fred;
  7. end;  
Best regards
paweld

 

TinyPortal © 2005-2018