Recent

Author Topic: How to prevent text to be selected in tedit?  (Read 1050 times)

Ramses

  • New Member
  • *
  • Posts: 41
How to prevent text to be selected in tedit?
« on: March 05, 2021, 02:46:20 pm »
Hi!

I have a tedit that i use to show information, and i dont want the user to be able to select/copy text in it, is there an easy way to do that without disabled it (because the disable function gray out the text)?

Or maybe its more easier to replace the tedit by a label?

Thanks

ASerge

  • Hero Member
  • *****
  • Posts: 1768
Re: How to prevent text to be selected in tedit?
« Reply #1 on: March 05, 2021, 02:48:54 pm »
Or maybe its more easier to replace the tedit by a label?
Yes

Ramses

  • New Member
  • *
  • Posts: 41
Re: How to prevent text to be selected in tedit?
« Reply #2 on: March 05, 2021, 03:13:02 pm »
alright, i was thinking that there was an option somewhere to do that, but if there is no easy way to do that i will replace every tedit with labels..

thanks for yiour answer

winni

  • Hero Member
  • *****
  • Posts: 2290
Re: How to prevent text to be selected in tedit?
« Reply #3 on: March 05, 2021, 05:11:34 pm »
Hi!

If you prefer TEdit:

It has a property ReadOnly.
Setting this to true is disabling any changes.

Winni

Ramses

  • New Member
  • *
  • Posts: 41
Re: How to prevent text to be selected in tedit?
« Reply #4 on: March 05, 2021, 05:54:12 pm »
Hi!

If you prefer TEdit:

It has a property ReadOnly.
Setting this to true is disabling any changes.

Winni

read only prevent any change but dont prevent from select/copy text, so i used the label instead

Warfley

  • Sr. Member
  • ****
  • Posts: 428
Re: How to prevent text to be selected in tedit?
« Reply #5 on: March 05, 2021, 06:09:36 pm »
Also possible, disable the edit, this will "gray it out"
Code: Pascal  [Select][+][-]
  1. MyEdit.Enabled := False;
But it will also disable all events and possibly (I don't know right now) also disable updating the control, so you might need to temporary enable it to write stuff into it

winni

  • Hero Member
  • *****
  • Posts: 2290
Re: How to prevent text to be selected in tedit?
« Reply #6 on: March 05, 2021, 06:24:42 pm »
Also possible, disable the edit, this will "gray it out"
Code: Pascal  [Select][+][-]
  1. MyEdit.Enabled := False;
But it will also disable all events and possibly (I don't know right now) also disable updating the control, so you might need to temporary enable it to write stuff into it

He does not want that!

Try to read again!

wp

  • Hero Member
  • *****
  • Posts: 8301
Re: How to prevent text to be selected in tedit?
« Reply #7 on: March 05, 2021, 07:19:08 pm »
Could not yet find a way to prevent selecting by a double-click, but when you catch the keyboard events, at least OnKeyDown, and disable it (Key := 0) then the user cannot enter anything any more. Also pasting from the clipboard by pressing CTRL+V is disable this way.  You could also disable selecting by dragging the mouse when you call Abort in the OnMouseMove event:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   Key := 0;
  5. end;
  6.  
  7. procedure TForm1.Edit1Click(Sender: TObject);
  8. begin
  9.   Abort;
  10. end;
  11.  
  12. procedure TForm1.Edit1MouseMove(Sender: TObject; Shift: TShiftState; X,
  13.   Y: Integer);
  14. begin
  15.   Abort;
  16. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4225
    • Bart en Mariska's Webstek
Re: How to prevent text to be selected in tedit?
« Reply #8 on: March 05, 2021, 08:13:33 pm »
Could not yet find a way to prevent selecting by a double-click,

On double click set SelLength to zero?

All this of course is a hack.

Bart

wp

  • Hero Member
  • *****
  • Posts: 8301
Re: How to prevent text to be selected in tedit?
« Reply #9 on: March 05, 2021, 09:58:00 pm »
Yes, I thought if that. But the selection goes away and comes back again. Strange...
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 4362
Re: How to prevent text to be selected in tedit?
« Reply #10 on: March 05, 2021, 11:46:48 pm »
This may work cross platform wise, I can't say but anyways, this will reject a copy paste operation. It may look and feel like its taking place but it gets rejected..

 Also you can replace the popup menu for the edit with a dummy one, that way the system's menu does not come up either..

I can't say if this works well in Lazarus across the board but it something I've done in Delphi...

You need to set the variable to TRUE to reject the operation... otherwise it behaves normally..

This only works per unit/Form page.. of course..

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13. TEDIT = Class(stdCtrls.TEDIT)
  14.   procedure HandlePaste(Var Msg:TMessage); Message WM_Paste;
  15.   procedure HandleCopy(Var Msg:TMessage); Message WM_Copy;
  16.   Public
  17.    RejectClipBoardOperations:Boolean;
  18. end;
  19.  
  20.   TForm1 = class(TForm)
  21.     Edit1: TEdit;
  22.   private
  23.  
  24.   public
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35. Procedure TEdit.HandleCopy(Var msg:TMessage);
  36. Begin
  37.   if rejectClipBoardOperations Then
  38.      Msg.Result := 1 else Inherited;
  39. end;
  40.  
  41. Procedure TEdit.HandlePaste(Var Msg:TMessage);
  42. Begin
  43.   If rejectClipboardOperations Then
  44.             Msg.Result := 1 Else Inherited;
  45. end;
  46.  
  47. end.
  48.  
  49.  

Don't forget to set the variable at runtime somewhere on the EDITS you want this behavior..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018