Custom format on Clipboard

F

fred

I am trying to Copy and paste using a custom format but I canot retrieve the
data.
In the main Form's class I have declared:

Dim myDocmanFormat as DataFormats.Format

then in the New Sub of the main form I have:

myDocmanFormat = DataFormats.GetFormat("DocmanFormat")

In the Copy sub I have:

Dim thisDataObject As New DataObject
Dim thisData As DocmanData = CType(thisNode.Tag, DocmanData).Clone
thisDataObject.SetData(Me.myDocmanFormat.Name, thisData)
Clipboard.SetDataObject(thisDataObject, True)

When I step through this part of the Copy routine "thisData" is definately
not Nothing. In the watch window I can access all of the properties of
"thisData".
In the Paste routine I have:

If Clipboard.GetDataObject.GetDataPresent(Me.myDocmanFormat.Name) Then
Dim myClipData As IDataObject = Clipboard.GetDataObject()
Dim DataFromClip As DocmanData =
CType(myClipData.GetData(Me.myDocmanFormat.Name), DocmanData)

Although the data format is present it always returns Nothing, that is
"DataFromClip" remains Nothing after executing the last line.

Can someone please tell me what I have done wrong.

Thanks,
Fred
 
C

Craig Vick [MSFT]

Hi Fred,

I don't see anything obviously wrong. You need to make sure the class
DocmanData is serializable. Here's some sample test code I threw together
that seems to work.

<Serializable()> _
Public Class TestObject

Private m_Value As String

Public Sub New(ByVal testValue As String)
m_Value = testValue
End Sub


Public Property TestValue() As String
Get
Return m_Value
End Get
Set(ByVal Value As String)
m_Value = Value
End Set
End Property
End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "


Private Sub buttonCopy_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles buttonCopy.Click
Dim test As New TestObject("Hello, this is a test.")
Dim testFormat As DataFormats.Format =
DataFormats.GetFormat("MyTestFormat")
Dim dataObj As New DataObject
dataObj.SetData(testFormat.Name, False, test)
Clipboard.SetDataObject(dataObj)
End Sub

Private Sub buttonShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles buttonShow.Click
Dim dataObj As DataObject = Clipboard.GetDataObject()
Dim testFormat As DataFormats.Format =
DataFormats.GetFormat("MyTestFormat")
If dataObj.GetDataPresent(testFormat.Name, False) Then
Dim test As TestObject =
CType(dataObj.GetData(testFormat.Name), TestObject)
MsgBox(test.TestValue)
Else
MsgBox("No data present")
End If
End Sub
End Class

Perhaps you can use this to see what's different in your case.

Craig VB.Net Team
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top