Clipboard and user defined data type

Salat

Regular
Joined
Mar 5, 2002
Messages
81
Location
Poland, Silesia
Is it possible to use clipboard to hold some my own defined data?

eg:
Visual Basic:
Structure stData
   Dim a as Integer
   Dim b as String
End Structure
Dim ToHold(10) as stData

'procedure code
Clipboard.SetDataObject(ToHold(2))

'other procedure code
ToHold(3) = Clipboard.GetDataObject()

there is some expection with 'invalid cast', I've tried with CType and DirectCast but I can't handle it... please help me !!!
 
You need your Structure to be serializable: change the declaration
to this:
Visual Basic:
<Serializable()> _
Structure stData
   Dim a As Integer
   Dim b As String
End Structure
Also, you need to use the GetData method of the IDataObject
returned by that GetDataObject method.
Visual Basic:
Dim cbData As DataObject = Clipboard.GetDataObject()

If cbdata.GetDataPresent(GetType(stData).ToString) Then _
            ToHold(3) = cbdata.GetData(GetType(stData).ToString, True)
 
I still can't handle this.... would You mind if I paste some code:
Visual Basic:
Class Form2
    Public Enum typeTypObiektu
        PoleTekstowe = 0
        PoleWyboru = 1
        PoleOpcji = 2
    End Enum
    Public Structure typePodobiektyObiektu
        Dim Top As Integer
        Dim Left As Integer
    End Structure
    <Serializable()> _
    Public Structure typeObiektStrony
        Dim Nazwa As String
        Dim Rodzaj As typeTypObiektu
        Dim Text As String
        Dim Kolor As Color
        Dim Czcionka As Font
        Dim Wyrównanie As StringAlignment
        Dim PodObiekty() As typePodobiektyObiektu
    End Structure
    Public Structure typeStrony
        Dim Nazwa As String
        Dim BackColor As Color
        Dim Obiekty() As typeObiektStrony
    End Structure
    Public Structure typedc
        Dim Tytu³ As String
        Dim Autor As String
        Public Edytowalny As Boolean
        Dim Strony() As typeStrony
    End Structure
    Public DC As typedc
' up there it is array in array in array...
' somewhere in the procedures of this class every array of this giant structure is redimed

Class Form1
'procedure 'Copy'
Dim d As Form2.typedc = DirectCast(Me.ActiveMdiChild, Form2).DC
Clipboard.SetDataObject(d.Strony(Akt).Obiekty(AktO))

'procedure 'Paste'
Dim d As Form2.typedc = DirectCast(Me.ActiveMdiChild, Form2).DC
Dim lo As Integer = d.Strony(Akt).Obiekty.Length
ReDim Preserve d.Strony(Akt).Obiekty(lo)
'here Your code
Dim cbData As DataObject = Clipboard.GetDataObject()
If cbData.GetDataPresent(GetType(Form2.typeObiektStrony).ToString) Then
     d.Strony(Akt).Obiekty(lo) = cbData.GetData(GetType(Form2.typeObiektStrony).ToString, True)
End If
DirectCast(Me.ActiveMdiChild, Form2).DC = d

There is no error, but new array of Obiekt should be created, and it is, but with every sub array and data set to Nothing. I realy don't get it.

Thank You for help...
 
when i call this data after pasting:
DC.Strony(0).Obiekt(10).Nazwa
the string Nazwa is set to Nothing, but Obiekt(10) was pasted from clipboard minute ago and the Nawz of copied one was not Nothing... so the Obiekt wasn't pased or copied properly...
 
Are you sure that d.Strony(Akt).Obiekty(AktO) contains valid data
when it is being sent to the clipboard?
 
You must make sure that the structure which needs to go on the
clipboard has the Serializable() attribute. Perhaps you should try
putting it on all of the Structures to see if that fixes anything.
 
Serializable() makes it so that the class can be converted into a form
that can be stored or transferred easily (quite often it is XML or, in
this case, binary). If the Serializable() attribute is off, then the program
has no way of putting the class into the clipboard, so you need to
turn it on.
 
Back
Top