[VB.Net] Probleem met gegevens uit clipboard halen

Pagina: 1
Acties:

  • abeker
  • Registratie: Mei 2002
  • Laatst online: 07-08 15:52
Ik ben aan het experimenteren met het Clipboard object, maar helaas loop ik daar tegen een aantal problemen op. Het lukt me wel om een object op het clipboard te plaatsen, maar bij het eraf halen onstaan wat problemen.

Als ik een object van het clipboard probeer af te halen met:
Visual Basic:
1
Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject()


En daarna opvraag of het type object voldoet aan wat ik er opgezet heb:
Visual Basic:
1
If myRetrievedObject.GetDataPresent("myFormat") Then MsgBox("myFormat Present")

Dan wordt het bericht netjes weergegevens. Als ik vervolgens GetData("myFormat") op myRetrievedObject aanroep, dan krijg ik echter niet het object terug dat ik erop gezet heb. Sterker nog, ik krijg helemaal geen object terug!

Ten einde raad heb ik het volgende voorbeeld van MSDN afgehaald:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Public Sub MyClipboardMethod()
' Creates a new data format.
    Dim myFormat As DataFormats.Format = _
            DataFormats.GetFormat("myFormat")

    ' Creates a new object and store it in a DataObject using myFormat 
    ' as the type of format. 
    Dim myObject As New MyNewObject()

    myObject.MyObjectValue = "Hallo!"

    Dim myDataObject As New DataObject(myFormat.Name, myObject)

    ' Copies myObject into the clipboard.
    Clipboard.SetDataObject(myDataObject)

    ' Performs some processing steps.
    ' Retrieves the data from the clipboard.
    Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject()

    If myRetrievedObject.GetDataPresent("myFormat") Then _
                MsgBox("myFormat Present")

    If IsNothing(myRetrievedObject.GetData(myFormat.Name)) Then _
                MsgBox("No data present?!?!")

    ' Converts the IDataObject type to MyNewObject type. 
    Dim myDereferencedObject As MyNewObject = _
            CType(myRetrievedObject.GetData(myFormat.Name), MyNewObject)

    ' Print the value of the Object in a textBox.
    TextBox1.Text = myDereferencedObject.MyObjectValue
End Sub 'MyClipboardMethod


Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
' Creates a new type.
Public Class MyNewObject
    Inherits Object
    Private myValue As String

    ' Creates a default constructor for the class.
    Public Sub New()
        myValue = "This is the value of the class"
    End Sub 'New

    ' Creates a property to retrieve or set the value.
    Public Property MyObjectValue() As String
        Get
            Return myValue
        End Get
        Set(ByVal Value As String)
            myValue = Value
        End Set
    End Property
End Class 'MyNewObject


Je zou zeggen dat een voorbeeld uit MSDN toch zou moeten werken, maar dat is helaas niet het geval... Het lukt me overigens wel om een string op het clipboard te plaatsen en eraf te halen, maar met eigen objecten gaat het fout. Iemand een idee waar het aan ligt??

the less one forgets, the less one remembers


Verwijderd

Voorgaande code werkte in de beta versie van visual studio. Je dient nu het Serializable attribuut mee te geven:

<Serializable()> Public Class MyNewObject

  • abeker
  • Registratie: Mei 2002
  • Laatst online: 07-08 15:52
Ah bedankt, dat was het inderdaad! Ik had dat ook al geprobeerd in mijn eigen code, maar dat werkte niet omdat een xmldocument niet serializable is :(

the less one forgets, the less one remembers