Cross dragging and dropping custom object type in C # WinForms

This question is close to what interests me, but not quite.

I have a .NET WinForms application written in C #. I have a ListView control that displays an array of C # objects. I hooked it up so that you can drag these list items into another form in one application and correctly pass an array of objects ( Session type) to the drop handler for that other form.

However, now I want to support drag and drop when I run multiple instances of my application. It seems that it will work (for example, GetDataPresent successfully), but it ultimately throws an exception when I really try to get the data - it cannot drop object[] in Session[] .

 if (e.Data.GetDataPresent("Fiddler.Session[]")) { Session[] oDroppedSessions; try { oDroppedSessions = (Session[])e.Data.GetData("Fiddler.Session[]"); } catch (Exception eX) { // reaches here } } 

Does anyone know if I should implement ISerializable for my objects to make this work? Usually I’m just trying to do this, but an ISerializable implementation for this class would be completely non-trivial, and I worry that these might be strange side effects.


UPDATE : the implementation of ISerializable does not help - the method is never called. Similarly, adding a Serializable attribute to a class has no effect. Any other ideas?

+7
c # winforms drag-and-drop
source share
3 answers

You cross the border of a process; references to objects are invalid in another process. The DataObject class supports serializing objects to get them through the wall; it uses the BinaryFormatter. So yes, you need to apply the [Serializable] attribute to your class and make sure that your objects can be correctly decorated.

+4
source share

Well, this is a shot, instead of using an entire array of sessions, try to do it individually, like this ...

    Session [] oDroppedSessions;
    try
    {
       if (e.Data.GetData ("Fiddler.Session []")! = null) {
           object [] objs = e.Data.GetData ("Fiddler.Session []");
           if (objs! = null && objs.Length> 1) {
              oDroppedSessions = new Session [objs.Length];
              int nIndex = 0;
              foreach (object obj in objs) {
                 if (obj is Session) {
                   oDroppedSessions [nIndex] = (Session) obj;
                   nIndex ++;
                 }
              }
           }
       }
    }
    catch (Exception eX)
    {// reaches here}

It’s worth a shot, except shooting in the leg, because I do not fully understand part of the session, try ...

Hope this helps, Regards, Tom.

0
source share

You can use β€œhow” for casting to avoid an exception (β€œhow” will return β€œzero” without throwing an exception if a failure is thrown) - but I don't think this will solve your problem (it will just avoid the actual exception), since I agree that you will need to make your class Serializable. You can test your hypothesis by commenting on fields that will be more difficult to make it work - for now, test it.

-one
source share

All Articles