Recent

Author Topic: How to allow users select text when TLabeledEdit is read only?  (Read 11078 times)

dzywendy

  • New member
  • *
  • Posts: 7
I don't want users to change the text, but I want them to be able to select the text and copy to clipboard if they want to, but once ReadOnly=true this is not possible. How can I do it? Thank you so much!
Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC 2.6.4, Carbon

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #1 on: July 17, 2015, 12:09:18 am »
I don't want users to change the text, but I want them to be able to select the text and copy to clipboard if they want to, but once ReadOnly=true this is not possible. How can I do it? Thank you so much!
And a copy-Button next to the Control doesn't work ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #2 on: July 17, 2015, 12:14:17 am »
Perhaps you could use a popup menu or set your own shortcut.

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #3 on: July 17, 2015, 12:14:54 am »
Which Lazarus version, which OS, which widgetset?

On Win7, win32, Lazarus 1.5, I can select text and copy from a TLabeledEdit regardless of the status of ReadOnly.

Bart

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #4 on: July 17, 2015, 12:19:46 am »
dzywendy, play a bit with this and you will find a way to make it work.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #5 on: July 17, 2015, 12:22:05 am »
First you could Use the onKeyPress event and
set the Key-variable to #0;
and
onChange-Event:
Code: [Select]
  LabeledEdit1.text := <Unchanged Text>;[Edit]
That pretty much makes your TLabeledit, and any other Editor-Control write-Protected, but you can still cursor around in the text.
« Last Edit: July 17, 2015, 12:23:58 am by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #6 on: July 17, 2015, 12:30:35 am »
[Correction]
The onKeyPress should look like
Code: [Select]
  if Key = #24 then
    Key := #3;     
  if Key <> #3 then
   Key := #0;         
otherwise the Ctrl-C key to copy is also disabled.
So is the Crtl-X, which is translated to Ctrl-C.
« Last Edit: July 17, 2015, 12:33:07 am by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #7 on: July 17, 2015, 12:38:50 am »
Which Lazarus version, which OS, which widgetset?

On Win7, win32, Lazarus 1.5, I can select text and copy from a TLabeledEdit regardless of the status of ReadOnly.

Bart
You are Right !
Also on Win7, win 32 Lazarus 1.4.2. So no need for some obscure event-handling.

OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

dzywendy

  • New member
  • *
  • Posts: 7
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #8 on: July 18, 2015, 09:23:00 am »
First you could Use the onKeyPress event and
set the Key-variable to #0;
and
onChange-Event:
Code: [Select]
  LabeledEdit1.text := <Unchanged Text>;[Edit]
That pretty much makes your TLabeledit, and any other Editor-Control write-Protected, but you can still cursor around in the text.

jc99, Thank you so much. You really helped me a lot. Although when wrote the exact code you gave me, it didn't work, but I really followed your idea and I finally got it to work. Thank you. This is the code that I have now.
Code: [Select]
procedure TForm1.RevSeqKeyPress(Sender: TObject; var Key: char);
begin
  key:=#0;//KeyPress only deals with characters on keyboard that has an ASCII value. This procedure prevents user from typing.
end;   

procedure TForm1.RevSeqKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState); //KeyDown deals with other keyboard operations, including tab, shift, command, control, etc.
begin
  if key<>67 then key:=0; //I'm using a Mac, and Command+C is equals to 67. I had to write a program to print the value.
end;

In this case, I only allowed copy operation, and didn't translate cut to copy. Because when I tried to do it, it didn't work. (for unknown reasons... weird.) Command+X, or cut, has a value of 88. If I write:
if key=88 then key:=67;
if key<>67 then key:=0;
I could still cut. I don't understand... but anyway, being able to copy is good enough for me. Thank you.
Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC 2.6.4, Carbon

dzywendy

  • New member
  • *
  • Posts: 7
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #9 on: July 18, 2015, 09:25:30 am »
Which Lazarus version, which OS, which widgetset?

On Win7, win32, Lazarus 1.5, I can select text and copy from a TLabeledEdit regardless of the status of ReadOnly.

Bart

I'm using Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC version 2.6.4. Unfortunately TLabeledEdit doesn't let user select text when read only, while TMemo allows selection when Readonly, which is a good thing.
Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC 2.6.4, Carbon

dzywendy

  • New member
  • *
  • Posts: 7
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #10 on: July 18, 2015, 09:27:56 am »
I don't want users to change the text, but I want them to be able to select the text and copy to clipboard if they want to, but once ReadOnly=true this is not possible. How can I do it? Thank you so much!
And a copy-Button next to the Control doesn't work ?
I tried it and it worked. But it's just ugly to have this extra "copy" button, and just it's not the way I wanted the application to work...
Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC 2.6.4, Carbon

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #11 on: July 18, 2015, 04:14:32 pm »
Code: [Select]
procedure TForm1.RevSeqKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState); //KeyDown deals with other keyboard operations, including tab, shift, command, control, etc.
begin
  if key<>67 then key:=0; //I'm using a Mac, and Command+C is equals to 67. I had to write a program to print the value.
end;

In this case, I only allowed copy operation, and didn't translate cut to copy. Because when I tried to do it, it didn't work. (for unknown reasons... weird.) Command+X, or cut, has a value of 88. If I write:
if key=88 then key:=67;
if key<>67 then key:=0;
I could still cut. I don't understand... but anyway, being able to copy is good enough for me. Thank you.
67 and 88 are the ASCII values of 'C' and 'X' so you also let these trough.
-> Try to hit <C> or <Shift>-<C>
to be sure you could still set the key to #0 and copy the text (selected text) in the method.
.. also to be able to compile the prog for other machines make a define like
Code: [Select]
{$ifdef darwin}
if key=88 or key=67 then
  edit.selection.copy; // This has to be adjusted a little
key:=0;
{$Else}
 if Key = #24 then
    Key := #3;     
  if Key <> #3 then
   Key := #0;
{$endif}
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

dzywendy

  • New member
  • *
  • Posts: 7
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #12 on: July 18, 2015, 08:58:00 pm »

67 and 88 are the ASCII values of 'C' and 'X' so you also let these trough.
-> Try to hit <C> or <Shift>-<C>
to be sure you could still set the key to #0 and copy the text (selected text) in the method.
.. also to be able to compile the prog for other machines make a define like
Code: [Select]
{$ifdef darwin}
if key=88 or key=67 then
  edit.selection.copy; // This has to be adjusted a little
key:=0;
{$Else}
 if Key = #24 then
    Key := #3;     
  if Key <> #3 then
   Key := #0;
{$endif}

I see. I didn't realize that 67 and 88 are just C and X! But with my code, I could not type C or X, only copy works. Is this because of something that has to do with the difference between KeyDown and KeyPress?
Also, what does the # sign in front of the numbers mean? (in Key = #24) How is it different from just doing key = 67?
When you say to have to the code work on other machine, did you mean PCs? I do want my code to work on PCs because people here use both platforms.
Thank you very much again!
Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC 2.6.4, Carbon

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #13 on: July 19, 2015, 12:43:01 am »
[...]
I see. I didn't realize that 67 and 88 are just C and X! But with my code, I could not type C or X, only copy works. Is this because of something that has to do with the difference between KeyDown and KeyPress?
Also, what does the # sign in front of the numbers mean? (in Key = #24) How is it different from just doing key = 67?
When you say to have to the code work on other machine, did you mean PCs? I do want my code to work on PCs because people here use both platforms.
Thank you very much again!
67 is just the number, #67 is the character number 67: 'C', Capital-C to be exact.
On PC the Control-Keys are coded from #1 - Ctrl-A (Sometimes Mark all) to #26 Ctrl-Z (EOF)
On some programs Ctrl-M #13 is equal to <Enter> (but apparently not to the browser)
You could make some tests, just make a brakepoint in the first line of OnKeyPress and inspect the value of key.
Maybe when you hit c  you get #99, but that's just a guess.

In KeyDown you get the Key-Value and the Shift-State, In Keypress, you get the correlated Character-Value.
  in the Shift-State you get the state of all Extra-Keys (Shift, Ctrl, Alt, and maybe the Apple-Command-Key)
« Last Edit: July 19, 2015, 12:49:05 am by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

Bart

  • Hero Member
  • *****
  • Posts: 5744
    • Bart en Mariska's Webstek
Re: How to allow users select text when TLabeledEdit is read only?
« Reply #14 on: July 19, 2015, 12:25:23 pm »
I'm using Mac OS X Yosemite 10.10.4, Lazarus 1.4.2, FPC version 2.6.4. Unfortunately TLabeledEdit doesn't let user select text when read only, while TMemo allows selection when Readonly, which is a good thing.

Please test with latest stable version (currently 1.4.2), or with trunk, and if the problem is still there then open a ticket about this in the bugtracker, so it gets the attention of the developers and can be fixed properly.
Always attach a sample application (sources only) that demonstrates the problem.

Bart

 

TinyPortal © 2005-2018