Clipboard

This forum is meant for questions and discussions about the X# language and tools
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Clipboard

Post 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?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Clipboard

Post 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
VR
Posts: 102
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

Clipboard

Post 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
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Clipboard

Post 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?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 4909
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Clipboard

Post 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?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Clipboard

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
Chris
Posts: 4909
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Clipboard

Post 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?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Clipboard

Post 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.
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Clipboard

Post 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
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Clipboard

Post 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!
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Post Reply