Lazarus

Programming => Operating Systems => Windows => Topic started by: Bert_Plitt on January 20, 2022, 02:17:55 am

Title: Writing text to clipboard produces error
Post by: Bert_Plitt on January 20, 2022, 02:17:55 am
Hello All:  I want to read and write text to the Windows clipboard in an application.  I tried a simple test: "Clipboard.AsText:='This is a test';, but this throws an error.  Demo is attached, including the error message.  Is there a bug or am I doing something wrong?  Any help appreciated!
Title: Re: Writing text to clipboard produces error
Post by: Thaddy on January 20, 2022, 05:12:00 am
Your project simply works here. (Windows 10 and Debian Linux too)
Code: Pascal  [Select][+][-]
  1. uses Clipbrd;
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. begin
  4.   Clipboard.AsText := 'Lazarus is great';
  5.   Edit1.Text := ClipBoard.AsText;
  6. end;
IOW, Your code should work and I can't explain the sigsev. What versions are you using? (Windows, FPC and Lazarus).
Title: Re: Writing text to clipboard produces error
Post by: Bert_Plitt on January 20, 2022, 04:57:11 pm
Thaddy - Versions I'm using:  Windows 10, ver 21H1; Lazarus 2.2.0 RC1; FPC 3.2.2, SVN ver 65419.  Besides having "ClipBrd" in the uses section of the unit file, are there any other items that need to be in the unit file, the *.lpi, or the *.lpr files?
Title: Re: Writing text to clipboard produces error
Post by: Thaddy on January 20, 2022, 06:28:53 pm
What Lazarus generates are the units you need, except clipbrd, that has to be added manually.
Note you use a release candidate instead of stable. I do not think that matters, but try a release version if you still have troubles.
After all I used almost exactly the same code as you wrote. I used trunk/trunk on Windows64 10 this morning.
Title: Re: Writing text to clipboard produces error
Post by: rvk on January 20, 2022, 06:59:47 pm
Your project simply works here. (Windows 10 and Debian Linux too)
Code: Pascal  [Select][+][-]
  1. uses Clipbrd;
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. begin
  4.   Clipboard.AsText := 'Lazarus is great';
  5.   Edit1.Text := ClipBoard.AsText;
  6. end;
IOW, Your code should work and I can't explain the sigsev. What versions are you using? (Windows, FPC and Lazarus).
Thaddy, Really strange that TS's code works for you. It really generates an error. You probably didn't take that code but made your own   ;D

@Bert_Plitt. You declared a Clipboard as TClipboard. If you are going to use it you NEED to initialize it. You didn't. That's why you are getting the error.

But... Clipboard is already a variable name in ClipBrd unit. So there is no need to declare it. You can just use it (it's already initialized in ClipBrd).

So this works.
Code: Pascal  [Select][+][-]
  1.   // Clipboard: TClipboard;   <-- comment this line
Title: Re: Writing text to clipboard produces error
Post by: Bert_Plitt on January 20, 2022, 07:05:31 pm
Thaddy - Yeah, I found the problem!  Got rid of the declaration:

var
  Clipboard: TClipboard;

Now it works perfectly!  Thanks for your efforts.
TinyPortal © 2005-2018