How to allow users select text when TLabeledEdit is read only? lazarus / 31-LCL ---------------------------------------------------------------------- dzywendy · 2015-07-16 22:00 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! ---------------------------------------------------------------------- jc99 · 2015-07-16 22:09 Quote from: dzywendy on July 17, 2015, 12:00:01 amI 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 ? ---------------------------------------------------------------------- typo · 2015-07-16 22:14 Perhaps you could use a popup menu or set your own shortcut. ---------------------------------------------------------------------- Bart · 2015-07-16 22:14 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 · 2015-07-16 22:19 dzywendy, play a bit with this and you will find a way to make it work. ---------------------------------------------------------------------- jc99 · 2015-07-16 22:22 First you could Use the onKeyPress event and set the Key-variable to #0; and onChange-Event: Code: [Select]  LabeledEdit1.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 · 2015-07-16 22:30 [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. ---------------------------------------------------------------------- jc99 · 2015-07-16 22:38 Quote from: Bart on July 17, 2015, 12:14:54 amWhich 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. ---------------------------------------------------------------------- dzywendy · 2015-07-18 07:23 Quote from: jc99 on July 17, 2015, 12:22:05 amFirst you could Use the onKeyPress event and set the Key-variable to #0; and onChange-Event: Code: [Select]  LabeledEdit1.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. ---------------------------------------------------------------------- dzywendy · 2015-07-18 07:25 Quote from: Bart on July 17, 2015, 12:14:54 amWhich 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. ---------------------------------------------------------------------- dzywendy · 2015-07-18 07:27 Quote from: jc99 on July 17, 2015, 12:09:18 amQuote from: dzywendy on July 17, 2015, 12:00:01 amI 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... ---------------------------------------------------------------------- jc99 · 2015-07-18 14:14 Quote from: dzywendy on July 18, 2015, 09:23:00 amCode: [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 or - 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} ---------------------------------------------------------------------- dzywendy · 2015-07-18 18:58 Quote from: jc99 on July 18, 2015, 04:14:32 pm 67 and 88 are the ASCII values of 'C' and 'X' so you also let these trough. -> Try to hit or - 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! ---------------------------------------------------------------------- jc99 · 2015-07-18 22:43 Quote from: dzywendy on July 18, 2015, 08:58:00 pm[...] 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 (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) ---------------------------------------------------------------------- Bart · 2015-07-19 10:25 Quote from: dzywendy on July 18, 2015, 09:25:30 amI'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 ---------------------------------------------------------------------- jc99 · 2015-07-19 11:28 Quote from: Bart on July 19, 2015, 12:25:23 pmPlease 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. [...] Are you sure ? There really bugs in the system ? And get the attention ? Or not ... or you are told to live with it ... ---------------------------------------------------------------------- JuhaManninen · 2015-07-19 12:07 Quote from: jc99 on July 19, 2015, 01:28:42 pmAre you sure ? There really bugs in the system ? Yes! There are now 1663 open issues for Lazarus project alone. "Hide Status : resolved (and above)" :   http://bugs.freepascal.org/view_all_bug_page.php QuoteAnd get the attention ? Or not ... or you are told to live with it ... No, but you may be asked to fix it yourself and provide a patch. Carbon and Cocoa widgetsets have many open bugs, contributions would be welcome. This is a volunteer based open source project. Anybody can fix bugs if they feel so. ---------------------------------------------------------------------- Bart · 2015-07-19 12:24 Quote from: jc99 on July 19, 2015, 01:28:42 pmAre you sure ? There really bugs in the system ? And get the attention ? Or not ... or you are told to live with it ... Why use such an offencing tone? (C'est le ton qui fait la musique...) Obviously the original poster has hit something that does not work as expected. Bugs repoted here are easily forgotten. Bugs reported in the bugtracker are not. That does not mean they are fixed instantly, once reported. Some bugs cannot be reproduced easily, some widgetsets have more active developers than others, some problems are harder than others, etc. etc. As Juha said: developing Lazaus (and hence fixing bugs) is done by volunteers, mostly in our spare time. It's easy to criticize, but it's harder to contribute. Bart ---------------------------------------------------------------------- jc99 · 2015-07-19 18:00 Quote from: Bart on July 19, 2015, 02:24:33 pmWhy use such an offencing tone? (C'est le ton qui fait la musique...) Sorry, i didn't want to offend anybody. Didn't you see the Smiley ? I only wanted to tickle a little. At first it looked like a normal question, but later it looked more, and more like a bug. But since i'm on PC Win/Linux I cannot contribute a patch. And until the problem is solved/patched, dzywendy needs a workaround. Which I offered. So I am actually contributing. PS.: I Was told, that a bug is actually none, and I have to live with it. (with more words of cause) [Edit] PPS: I know that in any none-trivial program there is nonzero amount of bugs. If somebody tells you otherwise, highly probable he's a lair, because proofing a program takes nearly infinity resources. (In normal circumstances you can't afford these ) There are only thoroughly tested programs. ---------------------------------------------------------------------- Leledumbo · 2015-07-19 18:36 Quote from: jc99 on July 19, 2015, 08:00:41 pmBut since i'm on PC Win/Linux I cannot contribute a patch. Umm...why can't you? ---------------------------------------------------------------------- rvk · 2015-07-19 19:01 Quote from: Leledumbo on July 19, 2015, 08:36:43 pmQuote from: jc99 on July 19, 2015, 08:00:41 pmBut since i'm on PC Win/Linux I cannot contribute a patch. Umm...why can't you? Because on Windows/Linux there is no problem (and selecting text in a read-only TLabeledEdit is no problem at all on Windows). And I can imagine creating a patch for an OS on which you can't test is really hard to do. ---------------------------------------------------------------------- jc99 · 2015-07-19 19:17 Quote from: rvk on July 19, 2015, 09:01:39 pmQuote from: Leledumbo on July 19, 2015, 08:36:43 pmUmm...why can't you? Because on Windows/Linux there is no problem (and selecting text in a read-only TLabeledEdit is no problem at all on Windows). And I can imagine creating a patch for an OS on which you can't test is really hard to do. Dito, and submitting a patch you can't test is no good style either. [Edit] I will contribute patches, but on this specific problem it's something different. I even looked if i could get an OS x running in a VM - But so far I only found solutions with VM-Workstation which you has to be patched to support OS x  and then ... and then ... and hope that all is ok ... ---------------------------------------------------------------------- dzywendy · 2015-07-21 19:30 Quote from: jc99 on July 19, 2015, 12:43:01 am67 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 (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) I see! I got it. Thank you. I finally got access to a PC and I debugged the program. I just set the TLabeledEdit to readonly, and all problem solved. So putting all the KeyDown and KeyPress in a {$ifdef darwin} should do it. It's so much easier in Windows to deal with this. ---------------------------------------------------------------------- dzywendy · 2015-07-21 19:40 Quote from: JuhaManninen on July 19, 2015, 02:07:47 pm No, but you may be asked to fix it yourself and provide a patch. Carbon and Cocoa widgetsets have many open bugs, contributions would be welcome. This is a volunteer based open source project. Anybody can fix bugs if they feel so. I wish I'm advanced at this enough to write a patch! Although knowing Free Pascal from before, This is actually my first Lazarus application with GUI. jc99 definitely contributed by helping me with this bug! It shouldn't be something too hard to fix. Programming is just a hobby for me and I've never had computer science courses in college, but I plan to do it more, and maybe be one of the volunteers later.