Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Clipboard 08 Jan 2022 20:23 #21040

  • FFF


  • Posts: 1263
  • Guys,
    had a go with the .net way of reading clipboard, e.g.:
    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 (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Clipboard 08 Jan 2022 22:20 #21041

    • ic2


  • Posts: 1220
  • 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:
    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

    Please Log in or Create an account to join the conversation.

    Clipboard 09 Jan 2022 10:35 #21042

    • VR


  • Posts: 51
  • Hi.

    with this change, it should work...
    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

    Please Log in or Create an account to join the conversation.

    Clipboard 09 Jan 2022 12:09 #21043

    • FFF


  • Posts: 1263
  • 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 (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Last edit: by FFF.

    Clipboard 09 Jan 2022 16:01 #21052

    • Chris
    • Chris's Avatar


  • Posts: 2955
  • Hi Karl,

    I checked my code in XIDE, I am using this and it seems to always work:
    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?
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Clipboard 09 Jan 2022 16:12 #21053

    • FFF


  • Posts: 1263
  • 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 ;-)
    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 (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Last edit: by FFF.

    Clipboard 09 Jan 2022 23:10 #21061

    • Chris
    • Chris's Avatar


  • Posts: 2955
  • 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?
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Clipboard 09 Jan 2022 23:26 #21063

    • FFF


  • Posts: 1263
  • 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 (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Clipboard 10 Jan 2022 13:06 #21067

    • ic2


  • Posts: 1220
  • Hello Karl,

    Could this be the solution?

    coderedirect.com/questions/461205/c-shar...ll-but-cant-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

    Please Log in or Create an account to join the conversation.

    Clipboard 10 Jan 2022 13:43 #21068

    • FFF


  • Posts: 1263
  • 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 (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Clipboard 11 Jan 2022 19:04 #21076

    • ic2


  • Posts: 1220
  • Hello Karl,

    Hmm, I thought I posted a reply...

    Here it is again:

    Glad I could help. It makes me wonder however why it works for me (and Chris I think) apparently without this statement?

    Dick

    Please Log in or Create an account to join the conversation.

    Clipboard 11 Jan 2022 23:49 #21083

    • Chris
    • Chris's Avatar


  • Posts: 2955
  • Hi Dick,

    I do have it in my Start() function, actually this attribute is needed for dozens of different things to work, so I already had it.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Clipboard 12 Jan 2022 15:02 #21087

    • ArneOrtlinghaus
    • ArneOrtlinghaus's Avatar


  • Posts: 286
  • We needed this attribute also for some situations that I do not remember anymore. We have it also at the beginning of the start function of the main programs.

    [STAThread] ;
    function Start as void
    ...

    Arne

    Please Log in or Create an account to join the conversation.

    Clipboard 13 Jan 2022 12:36 #21099

    • Fabrice
    • Fabrice's Avatar


  • Posts: 164
  • Here is a good explanation about this attribute, and the fact that it is needed for Clipboard support.
    docs.microsoft.com/en-us/archive/blogs/j...s-stathread-required

    Fab

    Please Log in or Create an account to join the conversation.

    Clipboard 13 Jan 2022 15:24 #21103

    • FFF


  • Posts: 1263
  • Hi Fabrice,
    thx for the link. If i understand this correctly, the attribute has to be set, no way out of this. Couldn't the compiler throw a warning, if one instantiates a clipboard without having the attribute?
    Regards
    Karl (X# 2.7; Xide 1.27; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Last edit: by FFF.
    • Page:
    • 1