Recent

Author Topic: Writing text to clipboard produces error  (Read 3300 times)

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Writing text to clipboard produces error
« 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!
Windows 10, Lazarus 2.2.2, FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Writing text to clipboard produces error
« Reply #1 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).
« Last Edit: January 20, 2022, 06:54:00 am by Thaddy »
Specialize a type, not a var.

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: Writing text to clipboard produces error
« Reply #2 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?
Windows 10, Lazarus 2.2.2, FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Writing text to clipboard produces error
« Reply #3 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.
Specialize a type, not a var.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Writing text to clipboard produces error
« Reply #4 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

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: Writing text to clipboard produces error
« Reply #5 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.
« Last Edit: January 20, 2022, 07:09:55 pm by Bert_Plitt »
Windows 10, Lazarus 2.2.2, FPC 3.2.2

 

TinyPortal © 2005-2018