Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:20:55+00:00 2026-06-12T11:20:55+00:00

I have an object which, although it has a text representation (i.e. could be

  • 0

I have an object which, although it has a text representation (i.e. could be stored in a string of about 1000 printable characters), is expensive to generate. I also have a tree control which shows “summaries” of the objects. I want to drag/drop these objects not only within my own application, but also to other applications that accept CF_TEXT or CF_UNICODETEXT, at which point the textual representation is inserted into the drop target.

I’ve been thinking of delaying the “rendering” the text representation of my object so that it only takes place when the object is dropped or pasted. However, it seems that Winforms is eagerly calling the GetData() method at the start of the drag, which causes a painful multi-second delay at the start of the drag.

Is there any way ensure that the GetData() happens only at drop time? Alternatively, what is the right mechanism for implementing this deferred drop mechanism in a Winforms program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:20:56+00:00Added an answer on June 12, 2026 at 11:20 am

    After some research, I was able to figure out how to do this without having to implement the COM interface IDataObject (with all of its FORMATETC gunk). I thought it might be of interest to others in the same quandary, so I’ve written up my solution. If it can be done more cleverly, I’m all eyes/ears!

    The System.Windows.Forms.DataObject class has this constructor:

    public DataObject(string format, object data)
    

    I was calling it like this:

    string expensive = GenerateStringVerySlowly();
    var dataObject = new DataObject(
        DataFormats.UnicodeText,
        expensive);
    DoDragDrop(dataObject, DragDropEffects.Copy);
    

    The code above will put the string data into an HGLOBAL during the copy operation. However, you can also call the constructor like this:

    string expensive = GenerateStringVerySlowly();    
    var dataObject = new DataObject(
        DataFormats.UnicodeText,
        new MemoryStream(Encoding.Unicode.GetBytes(expensive)));
    DoDragDrop(dataObject, DragDropEffects.Copy);
    

    Rather than copying the data via an HGLOBAL, this later call has the nice effect of copying the data via a (COM) IStream. Apparently some magic is going on in the .NET interop layer that handles mapping between COM IStream and .NET System.IO.Stream.

    All I had to do now was to write a class that deferred the creation of the stream until the very last minute (Lazy object pattern), when the drop target starts calling Length, Read etc. It looks like this: (parts edited for brevity)

    public class DeferredStream : Stream
    {
        private Func<string> generator;
        private Stream stm;
    
        public DeferredStream(Func<string> expensiveGenerator)
        {
            this.generator = expensiveGenerator;
        }
    
        private Stream EnsureStream()
        {
            if (stm == null)
                stm = new MemoryStream(Encoding.Unicode.GetBytes(generator()));
            return stm;
        }
    
        public override long Length
        {
            get { return EnsureStream().Length; }
        }
    
        public override long Position
        {
            get { return EnsureStream().Position; }
            set { EnsureStream().Position = value; }
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            return EnsureStream().Read(buffer, offset, count);
        }
        // Remaining Stream methods elided for brevity.
    }
    

    Note that the expensive data is only generated when the EnsureStream method is called for the first time. This doesn’t happen until the drop target starts wanting to suck down the data in the IStream. Finally, I changed the calling code to:

    var dataObject = new DataObject(
        DataFormats.UnicodeText,
        new DeferredStream(GenerateStringVerySlowly));
    DoDragDrop(dataObject, DragDropEffects.Copy);
    

    This was exactly what I needed to make this work. However, I am relying on the good behaviour of the drop target here. Misbehaving drop targets that eagerly call, say, the Read method, say, will cause the expensive operation to happen earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object which has several properties that are set when the object
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
I have an object which I first want to rotate (about its own center)
I have a data object which has a base class with three derived classes,
I have object A which in turn has a property of type Object B
I have object A which contains multiple instances of object B, which in turn
I have this object which is an instance of a superclass. I want to
I have an object which contains models for my ASP.NET MVC web app. The
I have an object which needs to destroy itself. Can it be done? Is
I have an object which im converting into an array of coords which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.