tags:

views:

134

answers:

4

I'm reading words in a text box by simulating the key presses required to highligh and copy the text. When I'm done, I want the clipboard to be exactly how I found it.

I was hoping I'd be able to do something like this:

IDataObject clipboardBackup = Clipboard.GetDataObject();
Clipboard.Clear();
//Save other things into the clipboard here, etc//
Clipboard.SetDataObject(clipboardBackup);

But that doesn't seem to work. It looks like you can go the route of specifically trying for text, audio, pictures, etc. and then saving them accordingly. (I guess 'data object' is specialized like that in my example too, I was hoping it was generic.)

I'd prefer not to use cases for every possible type of clipboard data, both to be more concise and to make sure I never lose the data regardless of format.

Any tips for grabbing any and all of the clipboard and then restoring it?

A: 

I'd consider the requirement for restoring the clipboard at all, if I were in your position. More often than not, the amount of data on the clipboard is going to be relatively small (a block of text, an image from a website, something like that), but every now and then you'll come across a situation where the user has placed a large amount of data on the clipboard, possibly some time ago, and possibly no longer required. Your app will have to be robust enough to cope with temporarily storing all that data during the lifetime of your operation. I personally don't think it is likely to be worth the effort, although you know the situation better than I could.

In short, don't. Just clear the clipboard with Clipboard.Clear(); after you're done.

ZombieSheep
Not my downvote, but doing this *will* make users hate the application. The clipboard and data on it belongs to the user, not any arbitrary app that feels like using it.
AakashM
Yes, I agree in principle, but this whole area of using the clipboard is a very thorny area - I know because I've had to do it in the past. In the end the sacrifice of a single piece of data on the clipboard was deemed worth it in my case. That's why I advocate the OP doing further analysis to make sure the need is actually there before tearing his/her hair out.
ZombieSheep
+2  A: 

Do not use the clipboard unless directly instructed to do so by the logged-on user. It's not for general application storage - it's for the user to use for what they want to clip.

If you want to get text from an arbitrary textbox, locate the window (in the Win32) sense and send an EM_GETTEXT Win32 message.

AakashM
A: 

The following article presents some code to backup and restore the Clipboard. There's more to it than one might imagine, so I won't repost the code here.

Clipboard Backup In C#

Winston Smith
A: 

It is impossible to fully restore the clipboard in cases where delayed rendering is used, or where the clipboard data actually contains pointers back into the local program that are not intended for other programs to use them (dumb, but I've seen it). Consider the classic example of Excel, using delayed rendering to offer the same selection in dozens of different formats, including hazardous ones like Bitmap and HTML (could consume hundreds of MB and several minutes of clock time for rendering, if thousands of cells are copied). And then there is the whole situation of other clipboard viewers reacting to your clipboard manipulations. They'll get duplicate data, altered data, or notification that the clipboard has been erased.
I think this sums it up best:

“Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.”
— Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992

Chris Thornton