Recent

Author Topic: Memo1.CopyToClipboard  (Read 15304 times)

eric

  • Sr. Member
  • ****
  • Posts: 267
Memo1.CopyToClipboard
« on: July 06, 2013, 09:00:38 am »
I've got what seems a trivial problem with this function. I have a memo with a few lines of text in it, and a button for the user to click to copy the text to the clipboard. First I wrote this:

Code: [Select]
procedure TMainForm.ClipButtonClick(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.SelLength := 0;
end;

but it doesn't copy anything to the clipboard. If I remove the third line then it works but the text remains highlighted. It looks as if the third line gets implemented before the second line completes its work.

I can work around the problem with the following rather clumsy code:

Code: [Select]
procedure TMainForm.ClipButtonClick(Sender: TObject);
var
  N: Integer;
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  for N := 1 to 10 do
  begin
    Delay(50);
    Application.ProcessMessages;
  end;
  Memo1.SelLength := 0;
end;

but is there a better way?

This is on Linux 64-bit + KDE and I haven't tested it on other systems, but I do need it to work equally on both Windows and Linux.


exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Memo1.CopyToClipboard
« Reply #1 on: July 06, 2013, 09:46:20 am »
Try to use TClipboard:
---(before use ->my_clipboard:= TClipboard.Create)
--eg
my_clipboard.Clear;
  curr_text:= Memo1.Lines.Text;//curr_text : String
  if my_clipboard.HasFormat(CF_Text) then
    my_clipboard.AsText:= curr_text;
It works!
Regards
« Last Edit: July 06, 2013, 10:09:27 am by exdatis »

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Memo1.CopyToClipboard
« Reply #2 on: July 06, 2013, 11:23:51 am »
Thank you, I'll have a look at it.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Memo1.CopyToClipboard
« Reply #3 on: July 06, 2013, 11:38:38 am »
Strange. Shouldn't CopyToClipboard just work?

@Eric: what Lazarus version are you using? Perhaps upgrading will help?

FYI, on Windows with Lazarus trunk your original code
Code: [Select]
procedure TMainForm.ClipButtonClick(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.SelLength := 0;
end;
copies to the clipboard ok...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Memo1.CopyToClipboard
« Reply #4 on: July 06, 2013, 11:41:39 am »
I suppose that problem is Linux,
I use TClipboard on win and Linux,
without problem.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Memo1.CopyToClipboard
« Reply #5 on: July 06, 2013, 12:27:30 pm »
If you have the latest stable version (or trunk version) of Lazarus on Linux and it shows the problem, perhaps it's a good idea to file a bug report (if one hasn't been filed already) so this can be fixed.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Memo1.CopyToClipboard
« Reply #6 on: July 06, 2013, 01:58:54 pm »
If you have the latest stable version (or trunk version) of Lazarus on Linux and it shows the problem, perhaps it's a good idea to file a bug report (if one hasn't been filed already) so this can be fixed.

I'll check that out. I'm using the latest released versions (Lazarus 1.0.10, FPC 2.6.2), and I've confirmed that the original code I quoted above works fine on Windows but malfunctions on both Linux 32-bit and 64-bit KDE (it may be a desktop issue rather than Linux per se).

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Memo1.CopyToClipboard
« Reply #7 on: July 06, 2013, 06:10:59 pm »
Code: [Select]
procedure TMainForm.ClipButtonClick(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.SelLength := 0;
end;

This works as expected on Linux (Fedora) 64 bit, GTK2

  regards,
      geno

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Memo1.CopyToClipboard
« Reply #8 on: July 06, 2013, 09:08:15 pm »
Thank you, but,
try to use several time, I've bad experiences on openSuSe, except when I use a clipboard manager, to clear clipboard.
Regards

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Memo1.CopyToClipboard
« Reply #9 on: July 07, 2013, 03:57:54 am »
hello,
it seems that selecting memo text on  linux has some bugs :
this doesn't work for me on Ubuntu 12.04 gtk2 :
Code: [Select]
Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.SelLength := 0;

it seems that the text isn't already selected when the copytoClipboard is executed.
if you have troubleshooting with :
Code: [Select]
Memo1.SelectAll;
  Memo1.CopyToClipboard;
  Memo1.SelLength := 0;

replace by this :
Code: [Select]
ClipBoard.AsText := Memo1.Text;     
Friendly, J.P
« Last Edit: July 07, 2013, 04:07:32 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: Memo1.CopyToClipboard
« Reply #10 on: July 07, 2013, 05:36:13 am »
I goofed.   :-[  After reading JP's post, I went back to double check myself, and found as he stated that it works if the text is pre-selected.  So my previous assumption that it works with gtk2 was incorrect.

    regards,
        geno


edit:
As eric pointed out in the begining, it works by using   .ProcessMessages

Code: [Select]

  Memo1.SelectAll;
  Application.ProcessMessages;
  Memo1.CopyToClipboard;
  Application.ProcessMessages;
  Memo1.SelLength := 0;

Another oddity, with just this code:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.SelectAll;
  Memo1.CopyToClipboard;
end;
Clicking the button twice will copy to clipboard correctly - but never if .SelLength := 0 is added at the end.

  regards,
     geno
« Last Edit: July 07, 2013, 06:15:32 am by geno »

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Memo1.CopyToClipboard
« Reply #11 on: July 07, 2013, 09:07:11 am »
I'm learning things from this thread. It hadn't occurred to me that Application.ProcessMessages on its own (without the delay loop) would fix the problem.

I've posted report ID 0024712 on the bugtracker.

[Edit] Application.ProcessMessages on its own doesn't fix the problem on my system. It needs the delay loop as well.
« Last Edit: July 07, 2013, 09:10:38 am by eric »

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: Memo1.CopyToClipboard
« Reply #12 on: July 07, 2013, 09:26:18 am »
I haven't tested this - its just a thought.....

How about instead of ProcessMessages .. Delay ....

Would PostMessage with a message handler for the event work?
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Memo1.CopyToClipboard
« Reply #13 on: July 07, 2013, 10:34:21 am »
This works, here:

Code: [Select]

uses Clipbrd; 
....
Memo1.SelectAll;
Clipboard.AsText:=Memo1.text;
Memo1.SelLength := 0;   

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: Memo1.CopyToClipboard
« Reply #14 on: July 07, 2013, 11:52:35 am »
Thanks Theo, that works here too.

(Actually, with this method it's not necessary to have the first and third lines at all. Clipboard.AsText := Memo1.Text is all that's needed.)

« Last Edit: July 07, 2013, 12:03:41 pm by eric »

 

TinyPortal © 2005-2018