Restoring Previous Clipboard Data
-
I am pasting image in a richTextBox using clipboard. It works fine. But I want to restore the previous clipped data. Any clue, How to go for it? The code I am using for trial is, as below
private void PasteImg(RichTextBox richTextBox1, string imageFileName)
{//Copy the previous clipped data System.Windows.Forms.DataObject prevDat = new DataObject(); prevDat.SetData(Clipboard.GetDataObject()); // #region Pasting the image if("" == imageFileName) imageFileName = @"c:\\JayShared\\music.bmp"; Bitmap myBitmap = new Bitmap(file); // Copy the bitmap to the clipboard. Clipboard.SetDataObject(myBitmap, false); // Get the format for the object type. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap); // After verifying that the data can be pasted, paste if(richTextBox1.CanPaste(myFormat)) { richTextBox1.Paste(); } #endregion Pasting the image // //paste back the previous clipped data to the clipboard Clipboard.SetDataObject(prevDat, true); //
}
-
I am pasting image in a richTextBox using clipboard. It works fine. But I want to restore the previous clipped data. Any clue, How to go for it? The code I am using for trial is, as below
private void PasteImg(RichTextBox richTextBox1, string imageFileName)
{//Copy the previous clipped data System.Windows.Forms.DataObject prevDat = new DataObject(); prevDat.SetData(Clipboard.GetDataObject()); // #region Pasting the image if("" == imageFileName) imageFileName = @"c:\\JayShared\\music.bmp"; Bitmap myBitmap = new Bitmap(file); // Copy the bitmap to the clipboard. Clipboard.SetDataObject(myBitmap, false); // Get the format for the object type. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap); // After verifying that the data can be pasted, paste if(richTextBox1.CanPaste(myFormat)) { richTextBox1.Paste(); } #endregion Pasting the image // //paste back the previous clipped data to the clipboard Clipboard.SetDataObject(prevDat, true); //
}
The major problem is that the managed
IDataObject
and OLE clipboard functions (which support drag-n-drop) don't support nearly as much as the OLEIDataObject
and related functionality. The best thing you can do is just store save theIDataObject
(don't store it in aDataObject
; you could loose a lot of data) and restore it later:IDataObject oldData = Clipboard.GetDataObject();
// Construct your new DataObject and set it, then later...
Clipboard.SetDataObject();You still run the risk of losing some data for certain applications that may sink events with the OLE implementation of the native
IDataObject
interface. Even the Office clipboard ring, for example, can't save everything (typically when the media type isTYMED_ISTREAM
orTYMED_ISTORAGE
because the provider may want to provided data to stream that may no longer be available depending on the connection to an advice sink). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
I am pasting image in a richTextBox using clipboard. It works fine. But I want to restore the previous clipped data. Any clue, How to go for it? The code I am using for trial is, as below
private void PasteImg(RichTextBox richTextBox1, string imageFileName)
{//Copy the previous clipped data System.Windows.Forms.DataObject prevDat = new DataObject(); prevDat.SetData(Clipboard.GetDataObject()); // #region Pasting the image if("" == imageFileName) imageFileName = @"c:\\JayShared\\music.bmp"; Bitmap myBitmap = new Bitmap(file); // Copy the bitmap to the clipboard. Clipboard.SetDataObject(myBitmap, false); // Get the format for the object type. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap); // After verifying that the data can be pasted, paste if(richTextBox1.CanPaste(myFormat)) { richTextBox1.Paste(); } #endregion Pasting the image // //paste back the previous clipped data to the clipboard Clipboard.SetDataObject(prevDat, true); //
}
Jay Shankar wrote: It works fine. But I want to restore the previous clipped data. Well, I'm not sure if that's your problem but when I execute your code I get a COMException (Error HRESULT E_FAIL has been returned from a call to a COM component) after
Clipboard.SetDataObject()
has been called but that might be due to the .NET Framework 2.0 (no, I don't have a version 1.x installed ;-)). Anyway. It should work fine when you replace the first two line with:IDataObject prevDat = Clipboard.GetDataObject();
This is also how it's done in the code sample in the Visual Studio Documentation (see Clipboard.GetDataObject Method[^]). Please let me know if this hint solves your problem. Best regards Dennis
-
The major problem is that the managed
IDataObject
and OLE clipboard functions (which support drag-n-drop) don't support nearly as much as the OLEIDataObject
and related functionality. The best thing you can do is just store save theIDataObject
(don't store it in aDataObject
; you could loose a lot of data) and restore it later:IDataObject oldData = Clipboard.GetDataObject();
// Construct your new DataObject and set it, then later...
Clipboard.SetDataObject();You still run the risk of losing some data for certain applications that may sink events with the OLE implementation of the native
IDataObject
interface. Even the Office clipboard ring, for example, can't save everything (typically when the media type isTYMED_ISTREAM
orTYMED_ISTORAGE
because the provider may want to provided data to stream that may no longer be available depending on the connection to an advice sink). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]Dear Heath, Thanks a lot for your reply. I modified the function as below and tested with copying text on Clipboard before entering into the function and after execution of the function. But the test result failed for 1.TextPad 2.Notepad 3. MS Word. I have modified the code as below.
IDataObject oldData = Clipboard.GetDataObject();
System.Windows.Forms.DataObject prevDat = new DataObject();prevDat.SetData(oldData);
//
//.....
//...code for pasting operation here
//.........
//Clipboard.SetDataObject(prevDat,true);
Can anything be done further? Or Should I modify the pasting of image in richTextBox functionality, which does not use Clipboard. Regards, Jay
-
Jay Shankar wrote: It works fine. But I want to restore the previous clipped data. Well, I'm not sure if that's your problem but when I execute your code I get a COMException (Error HRESULT E_FAIL has been returned from a call to a COM component) after
Clipboard.SetDataObject()
has been called but that might be due to the .NET Framework 2.0 (no, I don't have a version 1.x installed ;-)). Anyway. It should work fine when you replace the first two line with:IDataObject prevDat = Clipboard.GetDataObject();
This is also how it's done in the code sample in the Visual Studio Documentation (see Clipboard.GetDataObject Method[^]). Please let me know if this hint solves your problem. Best regards Dennis
Dear Dennis, Thanks a lot for your reply. I modified the function as par suggestion by you and Mr. Heath. But could not achieve the goal. Still trying.... Regards, Jay
-
Dear Heath, Thanks a lot for your reply. I modified the function as below and tested with copying text on Clipboard before entering into the function and after execution of the function. But the test result failed for 1.TextPad 2.Notepad 3. MS Word. I have modified the code as below.
IDataObject oldData = Clipboard.GetDataObject();
System.Windows.Forms.DataObject prevDat = new DataObject();prevDat.SetData(oldData);
//
//.....
//...code for pasting operation here
//.........
//Clipboard.SetDataObject(prevDat,true);
Can anything be done further? Or Should I modify the pasting of image in richTextBox functionality, which does not use Clipboard. Regards, Jay
I said do not store the old data in a managed
DataObject
. You keep it as it - as an interface reference. That has the greatest change of preserving the data that is there. Once again:IDataObject oldData = Clipboard.GetDataObject();
// Make your own DataObject. DO NOT TOUCH oldData
Clipboard.SetDataObject(oldData);This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
-
I said do not store the old data in a managed
DataObject
. You keep it as it - as an interface reference. That has the greatest change of preserving the data that is there. Once again:IDataObject oldData = Clipboard.GetDataObject();
// Make your own DataObject. DO NOT TOUCH oldData
Clipboard.SetDataObject(oldData);This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
It results
System.Runtime.InteropServices.ExternalException : The requested clipboard operation failed.
Error. Regards, Jay. -
It results
System.Runtime.InteropServices.ExternalException : The requested clipboard operation failed.
Error. Regards, Jay.On what line? What does your code look like? Please be more specific. I can't help you if you just give me tidbits of information. Why do you think doctors want you to come in in order to diagnose problems, besides charging you money (which we don't do here, so that's even more incentive to be verbose). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
-
On what line? What does your code look like? Please be more specific. I can't help you if you just give me tidbits of information. Why do you think doctors want you to come in in order to diagnose problems, besides charging you money (which we don't do here, so that's even more incentive to be verbose). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]
I am really sorry for not elaborating the scenario. Below are the details. Steps resulting error 1. I. Open TextPad / MS Word, II. write text III. Copy the content in the clipboard 2. I. Run the C# Application II. Call the method 3.Causes Runtime eroor at Clipboard.SetDataObject(oldData) line ; 4. Error Details : System.Runtime.InteropServices.ExternalException : The requested clipboard operation failed. Code :
private void PasteImg(RichTextBox richTextBox1, string imageFileName)
{
//Copy the previous clipped data
IDataObject oldData = Clipboard.GetDataObject();#region Pasting the image if("" == imageFileName) imageFileName = @"c:\\JayShared\\music.bmp"; Bitmap myBitmap = new Bitmap(imageFileName); // Copy the bitmap to the clipboard. Clipboard.SetDataObject(myBitmap, false); // Get the format for the object type. DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap); // After verifying that the data can be pasted, paste if(richTextBox1.CanPaste(myFormat)) { richTextBox1.Paste(); } #endregion Pasting the image // //paste back the previous clipped data to the clipboard Clipboard.SetDataObject(oldData); //.......... **Error occurs here.........** //
}