yamamoto124

I tried to get the windows clipboard value with any format, and send it back to the clipboard.

But the clipboard value was lost after the following code process.

for example, when you copy a text value to the clipborad, after the following code excution.

'=================================

'copy some data in advence

'get the clipboard data

Dim ClipBoardValue As IDataObject

ClipBoardValue = Clipboard.GetDataObject()

'Clear the clipboard data

Clipboard.Clear()

'set the data

Clipboard.SetDataObject(ClipBoardValue, True)

'======================================

Futhermore I modified the above code as followings.

I changed the IDataObject to DataObject. This code can handle any type of objects,

but if I executed the following code twice, it returns exception error code.

'===================================

'copy some data in advence

'get the clipboard data

Dim ClipBoardValue As DataObject

ClipBoardValue = Clipboard.GetDataObject()

'Clear the clipboard data
Clipboard.Clear()

'the below line causes a exception error at send time execution. Why

Clipboard.SetDataObject(ClipBoardValue, True)

'=====================================

Please tell me how to fix this code.



Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

Dave299

ClipBoardValue does not contain the data from the clipboard, it simply points to the data that is on the clipboard. So when you clear the clipboard the data it is pointing to is cleared. You should also be using DataObject rather than IDataObject.

Sommething like this seems to work ok:

Dim ClipBoardValue As DataObject = CType(Clipboard.GetDataObject(), DataObject)

Dim ClipBoardValue2 As New DataObject

Dim Txt As String

ClipBoardValue = CType(Clipboard.GetDataObject(), DataObject)

Txt = ClipBoardValue.GetData("Text", True).ToString

'Clear the clipboard data

Clipboard.Clear()

'set the data

ClipBoardValue2.SetData("Text", True, Txt)

Clipboard.SetDataObject(ClipBoardValue2, True)





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

tanaka333

Thanks for the reply. But the sample code seems to be able to handle only text data.

I want to hadle all sorts of data. I understood my sample code copy only the object reference.

How can I copy the DataObject





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

Dave299

The DataObject can store all sorts of data formats but to make use of them you have to specify what format you are using. If you look at the IDataInterface you will see that it only specifies four methods, all of which require you to specify the data format to be used. The example above used the "Text" format but it could just as easily have been an "Image" or any other supported format.

I don't think there is a way you can simply copy the data in a DataObject without knowing what format it is in (although I could be proved wrong)





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

tanaka333

But my second sample code can handl all sorts of object, although it causes error at seconde time execution.

How do you think why the second code can work only at first time exection





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

JohnWein

This works fine to get and set any format on the clipboard without knowing the original format.

Code Snippet

Dim ClipBoardDataObject As DataObject

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

ClipBoardDataObject = DirectCast(Clipboard.GetDataObject(), DataObject)

Clipboard.Clear()

Clipboard.SetDataObject(ClipBoardDataObject)

End Sub





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

Dave299

John

I'm curious as to how you tested this. I did the following which appears to show that it doesn't work

Public Class Form1

Dim ClipBoardValue As DataObject

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Clipboard.SetText("Test")

ClipBoardValue = CType(Clipboard.GetDataObject(), DataObject)

MsgBox(ClipBoardValue.GetText)

Clipboard.Clear()

Clipboard.SetDataObject(ClipBoardValue, True)

MsgBox(Clipboard.GetText)

End Sub

End Class





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

JohnWein

I checked to see what kind of data was on the clipboard, but didn't actually look at it. My bad.

Code Snippet

Dim ClipBoardDataObject As DataObject

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(Clipboard.ContainsAudio.ToString & " , " & Clipboard.ContainsFileDropList.ToString & " , " & Clipboard.ContainsImage & " , " & Clipboard.ContainsText)

ClipBoardDataObject = DirectCast(Clipboard.GetDataObject(), DataObject)

Clipboard.Clear()

MsgBox(Clipboard.ContainsAudio.ToString & " , " & Clipboard.ContainsFileDropList.ToString & " , " & Clipboard.ContainsImage & " , " & Clipboard.ContainsText)

Clipboard.SetDataObject(ClipBoardDataObject)

End Sub

Why does it report that the data is there when it isn't





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

Dave299

Can't really answer that one for certain.

I would guess that Clipboard.Clear only clears the data that is contained within the IDataObject that is stored on the clipboard. If it cleared the actual object then a line such as Clipboard.GetDataObject immediately after Clipboard.Clear would fail, but it doesn't. So the DataObject is still there but the data it contains has been set to Nothing. So, using my example the string would be set to an empty string, which is actually the case if you check it out.





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

JohnWein

I played with it some more it. The .Net class does things inconsistently. It lets you think you can do something but when you try it you get an error. I tried serializing the DataObject without success. Natively, Windows only lets you get and set the data by format. I think you can assume that you must do the same using the .Net classes.





Re: Visual Basic Express Edition How to get the clipboard value and return the value to the clipboard after some processes.

Dave299

Which, I think, is where I came in.