Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Restoring Previous Clipboard Data

Restoring Previous Clipboard Data

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Shankar
    wrote on last edited by
    #1

    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);
    
    
    //
    

    }

    H D 2 Replies Last reply
    0
    • J Jay Shankar

      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);
      
      
      //
      

      }

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 OLE IDataObject and related functionality. The best thing you can do is just store save the IDataObject (don't store it in a DataObject; 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 is TYMED_ISTREAM or TYMED_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]

      J 1 Reply Last reply
      0
      • J Jay Shankar

        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);
        
        
        //
        

        }

        D Offline
        D Offline
        Dennis C Dietrich
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • H Heath Stewart

          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 OLE IDataObject and related functionality. The best thing you can do is just store save the IDataObject (don't store it in a DataObject; 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 is TYMED_ISTREAM or TYMED_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]

          J Offline
          J Offline
          Jay Shankar
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • D Dennis C Dietrich

            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

            J Offline
            J Offline
            Jay Shankar
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • J Jay Shankar

              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

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              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]

              J 1 Reply Last reply
              0
              • H Heath Stewart

                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]

                J Offline
                J Offline
                Jay Shankar
                wrote on last edited by
                #7

                It results System.Runtime.InteropServices.ExternalException : The requested clipboard operation failed. Error. Regards, Jay.

                H 1 Reply Last reply
                0
                • J Jay Shankar

                  It results System.Runtime.InteropServices.ExternalException : The requested clipboard operation failed. Error. Regards, Jay.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  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]

                  J 1 Reply Last reply
                  0
                  • H Heath Stewart

                    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]

                    J Offline
                    J Offline
                    Jay Shankar
                    wrote on last edited by
                    #9

                    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.........**
                    
                    //
                    

                    }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups