CLIPBOARD: How to add several images and strings at once?

  • Thread starter Steven Van Dyke
  • Start date
S

Steven Van Dyke

Hi,

I have an array of items, each having an image and description. I would like
to programatically copy them all to the clipboard (for pasting into say a
Word document). I've tried the following code, thinking SetData keeps
appending info to the clipboard, but when I paste into Word, I only get the
last description from the last item in my list (no images). What am I doing
wrong here?

private void CopySelectedItemsToClipboard()
{
IDataObject ido = new DataObject();

foreach MyItems myItem in items)
{
if (myItem.selected)
{
ido.SetData(DataFormats.Bitmap, myItem.image);
ido.SetData(DataFormats.Text, myItem.description);
}
}

Clipboard.SetDataObject(ido, true);

Steve
 
M

Michael Phillips, Jr.

You can only store one clipboard format per instance of IDataObject.

That is why you only get the last text object in your loop on to the
clipboard.

Just change your loop to place one clipboard format at a time on to the
clipboard.
Each DataObject instance will hold one clipboard format.
 
A

Atul

When storing to the clipboard in the same format, the new data will
overwrite any existing data.

-Atul, Sky Software http://www.ssware.com
Shell MegaPack ActiveX & .Net
Drop In Windows Explorer like Shell Browsing GUI controls for your app.
 
O

Oliver Sturm

Steven said:
I have an array of items, each having an image and description. I would like
to programatically copy them all to the clipboard (for pasting into say a
Word document). I've tried the following code, thinking SetData keeps
appending info to the clipboard, but when I paste into Word, I only get the
last description from the last item in my list (no images). What am I doing
wrong here?

You are right about your assumption that SetData appends information (or
rather, data formats). Your code also looks good. What may be your
problem is that while the clipboard can contain multiple data formats at
the same time, only one of them will be accessed by another application.
The reason for this is that the data formats are meant to be different
representations of the same information, NOT different individual pieces
of information.

So when pasting something into Word, there's an algorithm in Word that
looks at the available formats on the clipboard and decides which one of
them to use. The data for this one format is then queried and pasted,
nothing more and nothing less. It's not possible to put more than one
item of data on the clipboard at the same time.

If you'd want to do this for your own application's purposes, it would
be possible to create a class encapsulating the various objects you want
to copy and put that on the clipboard. But obviously Word won't know
about that format, so it won't be any help in your situation. The only
thing you could do is look for a format that Word knows and that might
accomodate multiple data objects at the same time - maybe there's such a
thing because the MS guys felt the need for it at a point :)


Oliver Sturm
 

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