Recent

Author Topic: [SOLVED] Disallow selection TRichMemo  (Read 1665 times)

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
[SOLVED] Disallow selection TRichMemo
« on: September 25, 2021, 03:29:46 pm »
Hi guys,

I was looking into https://github.com/skalogryz/richmemo TRichMemo (that inherits from StdCtrls.TCustomMemo) and would like to known if it is possible to disallow text selection and focus.

An alternative is simply doing RichMemo.Enabled := False (However the background of a disabled control changes and for my use case it must be unchanged).

Considering this first alternative, I would need to override the paint method to avoid background changes (However, I could not find a proper way of doing it and I just want to make sure it is really possible for this sort of control).

Can you shed some light on this issue, please!

Best,
R
« Last Edit: September 27, 2021, 02:32:10 pm by cpicanco »

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #1 on: September 25, 2021, 03:42:30 pm »
I just found an answer on StackOverflow that overrides a TMemo on Delphi.   

https://stackoverflow.com/questions/55899526/how-to-draw-a-colored-line-to-the-left-of-a-tmemo-which-looks-like-a-gutter

I am considering to use the same technique in Lazarus. It should be possible, both have the WMPaint method.

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #2 on: September 25, 2021, 04:24:02 pm »
Well, I underestimate the complexity of this problem.   :D

Would really appreciate any comments on how to disallow selection and focus and keep the background unchanged on a TCustomMemo descendant.

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #3 on: September 25, 2021, 04:34:35 pm »
Well, now I am considering hooking the colors using this approach:

https://theroadtodelphi.com/2012/02/06/changing-the-color-of-edit-controls-with-vcl-styles-enabled/

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2710
    • havefunsoft.com
Re: Disallow selection and focus on TRichMemo
« Reply #4 on: September 26, 2021, 07:07:17 pm »
An alternative is simply doing RichMemo.Enabled := False (However the background of a disabled control changes and for my use case it must be unchanged).
are you trying to use RichMemo as a rich-text Label?
setting "enabled" to false, might also prevent "scrolling" of the text. (not sure if it's critical for you or not).

Maybe you simply want to set to "ReadOnly := true". It doesn't prevent the selection... but maybe you don't want to prevent the selection after all? :)
The obvious benefit is that you don't have to mess with colors anymore.

And you can always set SetLength to zero, for OnSelhange event.
« Last Edit: September 26, 2021, 10:50:03 pm by skalogryz »

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #5 on: September 27, 2021, 01:19:41 pm »
Quote
are you trying to use RichMemo as a rich-text Label?


Yes.

Quote
Maybe you simply want to set to "ReadOnly := true". It doesn't prevent the selection... but maybe you don't want to prevent the selection after all?

I did that. I need to prevent text selection and focus. Allowing text selection and focus may interfere with other controls that do requires focus. I am writing experiments for researchers and I really don't want participants playing around with the message presented.  :D

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #6 on: September 27, 2021, 01:33:02 pm »
For me, changing colors is not feasible. Too much work.

Can you confirm how complicated implementing a RichMemo.SelectableText and a RichMemo.WantFocus properties would be? I choose RichMemo because in some cases people may want a long message with vertical scroll and a simple label can't do that. In that case the control would receive focus with no text selection.


PS.:

Right now I am considering using https://wiki.delphi-jedi.org/wiki/JEDI_Visual_Component_Library for markup labels

« Last Edit: September 27, 2021, 02:42:21 pm by cpicanco »

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #7 on: September 27, 2021, 01:37:12 pm »
Quote
And you can always set SetLength to zero, for OnSelhange event.

Will try this alternative and see what happens.

cpicanco

  • Sr. Member
  • ****
  • Posts: 465
  • Programmer and Behavioral Scientist
    • Portfolio
Re: Disallow selection and focus on TRichMemo
« Reply #8 on: September 27, 2021, 02:31:33 pm »
For unselecting this code works great:

Code: Pascal  [Select][+][-]
  1.   if RichMemo1.SelLength > 0 then begin
  2.     RichMemo1.SelLength := 0;
  3.   end;  
  4.  


For removing the focus, I am afraid the issue is unresolved for Lazarus (at least in the last 7 years!):

https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/25396

I tried

  DefocusControl(RichMemo1, False);
  DefocusControl(RichMemo1, True);
  Self.ActiveControl := nil;

In different events and nothing seems to work when you have just one control in a form.

I will create another topic for that as needed.

 

TinyPortal © 2005-2018