Setting multiple formats on Clipboard

G

Guest

The help for the Windows.Forms.Clipboard class states, "Place data on the
clipboard in multiple formats to maximize the possibility that a target
application, whose format requirements you might not know, can successfully
retrieve the data." So how do you do that? I want to place text and an
image on the Clipboard at the same time. But, when I run the following test
code only the text is on the Clipboard:

Dim bmp As New Drawing.Bitmap(100, 100,
Imaging.PixelFormat.Format24bppRgb)
Call Windows.Forms.Clipboard.SetDataObject(bmp)
Call Windows.Forms.Clipboard.SetDataObject("1,2,3,4,5")

Am I doing something wrong?

Thanks,
Lance
 
P

Peter Huang [MSFT]

Hi

We can build a dataobject of multiple formats.
Here is the code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Creates a new data object, and assigns it the component.
Dim myDataObject As New DataObject

' Adds data to the DataObject, and specifies no format conversion.
myDataObject.SetData(DataFormats.UnicodeText, False, "My Unicode
data")
myDataObject.SetData(DataFormats.Text, False, "My Text")
myDataObject.SetData(DataFormats.Bitmap, False, New
Bitmap("c:\temp\test.jpg"))

Clipboard.SetDataObject(myDataObject)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim ido As IDataObject = Clipboard.GetDataObject()
Dim arrayOfFormats As String() = ido.GetFormats()
For Each str As String In arrayOfFormats
MsgBox(str)
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

I am glad that my suggestion helped you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thank you Peter, this is very helpful for me as well!

However, I have a further question: is it possible to set priorities on the
different data formats? I put CSV (in a memorystream) and plain text on the
clipboard, and when pasting to excel it seems to prefer the plain text over
the csv format. I really want it to use the CSV as default, but I can not
acheive this by manipulating excel, since we are restricted not to automate
or control other apps from the one I'm writing.

Also, it seems the memrystream looses encodings. Our users are swedish and
some of the characters in our code page are scrambled when pasting :-(

Any ideas?

br
Carolina
 

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