xsharp.eu • Clipboard
Page 1 of 2

Clipboard

Posted: Sat Jan 08, 2022 7:23 pm
by FFF
Guys,
had a go with the .net way of reading clipboard, e.g.:

Code: Select all

FUNCTION x() AS LOGIC
//VAR z := System.Windows.Forms.Clipboard.GetDataObject()
LOCAL z AS IDataObject
TRY
z := System.Windows.Forms.Clipboard.GetDataObject()

IF  z:GetDataPresent(DataFormats.Text)
?	z:GetData(DataFormats.Text)
ENDIF
CATCH e AS Exception
	? e
END
RETURN  TRUE
This throws a Nullreference exception on z.
I suspect this has to do with me using an interface while having no clue at all, what i do ;)

Any hint, please?

Clipboard

Posted: Sat Jan 08, 2022 9:20 pm
by ic2
Hello Karl,

I use X# comparable code for reading bitmaps but I check if GetDataObject is Null before assigning it, I think that makes the difference:

Code: Select all

Local data As System.Windows.Forms.IDataObject
....
If (System.Windows.Forms.Clipboard.GetDataObject() != Null)
	data := System.Windows.Forms.Clipboard.GetDataObject()
	If (data:GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
		image := (System.Drawing.Image)data:GetData(System.Windows.Forms.DataFormats.Bitmap, True)
....
Dick

Clipboard

Posted: Sun Jan 09, 2022 9:35 am
by VR
Hi.

with this change, it should work...

Code: Select all

FUNCTION x() AS LOGIC
   TRY
       VAR z := System.Windows.Forms.Clipboard.GetDataObject()
       IF z?:GetDataPresent(DataFormats.Text) == true
          ?  z:GetData(DataFormats.Text)
       ENDIF
   CATCH e AS Exception
	? e
   END
RETURN  TRUE

Clipboard

Posted: Sun Jan 09, 2022 11:09 am
by FFF
It works in so far, as the exception is gone ;-)
But z is always Null, whatever i copied to clipboard prior to calling the function?

Clipboard

Posted: Sun Jan 09, 2022 3:01 pm
by Chris
Hi Karl,

I checked my code in XIDE, I am using this and it seems to always work:

Code: Select all

LOCAL ido AS IDataObject
ido := Clipboard.GetDataObject()
IF ido:GetDataPresent(DataFormats.Text)
   cPaste := ido:GetData(DataFormats.Text):ToString()
...
If it's not working, then I guess you that the latest "Copy" operation you did in WIndows was not with text?

Clipboard

Posted: Sun Jan 09, 2022 3:12 pm
by FFF
Hi Chris,
tried "your" variant - NullReferenceExeption. I explicitely copied prior to check some text from editor with CTRL+C
Must be something silly here, as usual ;-)

Code: Select all

FUNCTION b() AS LOGIC
	LOCAL ido AS IDataObject
ido :=  System.Windows.Forms.Clipboard.GetDataObject()
IF ido:GetDataPresent(DataFormats.Text)
VAR   cPaste := ido:GetData(DataFormats.Text):ToString()
   ? cPaste
ENDIF
RETURN TRUE

Clipboard

Posted: Sun Jan 09, 2022 10:10 pm
by Chris
Ah indeed, I have actually that code surrounded with a TRY...AND TRY, so I am never getting an exception :)
As the guys said above, you (we) should be checking if GetDataObject() returns NULL false.
If there does exist some test in the clipboard, i can't think why it might not be working for you. Just to be sure, pasting the text in an editor with CTRL+V does work, right?

Clipboard

Posted: Sun Jan 09, 2022 10:26 pm
by FFF
Yes, i read that you get a null if clipboard is empty... But I tried with copied text, copied image, copied desktop link, I always get the null. Strange.
FTR, using VO's class, it works.

Clipboard

Posted: Mon Jan 10, 2022 12:06 pm
by ic2
Hello Karl,

Could this be the solution?

https://coderedirect.com/questions/4612 ... t-be-empty
According to this (and more) MSDN post(s) the Main method must be marked with the STAThreadAttribute attribute.Although it works fine for me without....

Also I wonder, if you see z is Null, if your program did reach "IF ido:GetDataPresent(DataFormats.Text)"?

In other words: is it recognized as a string but an empty one?

Dick

Clipboard

Posted: Mon Jan 10, 2022 12:43 pm
by FFF
Hi Dick!
Bingo. Added [STAThreadAttribute] and all is fine.
IIRC, in earlier times, this attribute was automatically added?

Well, would never have hunted in this direction ;-)
Thank you!