Drag and drop data between instances

I am trying to allow my users to drag specific rows of data from one custom list control to another, where the second list control is in another instance of the same application.

DoDragDrop(parameterTypedListView.SelectedObjects, DragDropEffects.Copy);

      

where parameterTypedListView.SelectedObjects

is a generic IList where T is a custom class containing only parameter values ​​as fields / properties.

In the OnDragDrop event, I try to fetch this data, but only get System.__ComObject

... an object that seems to inherit from System.MarshalByRefObject

.

In short: how can I extract data in an object oriented format that I can actually use?

Edit: Setting my custom class as serializable has no discernible effect. I can list __ComObject:

foreach (var dataObject in (IEnumerable) e.Data.GetData("System.Collections.ArrayList"))
{
    // this actually enumerates the correct number of times, i.e. as many times as there are items in the list.
}

      

but each dataObject is itself a System .__ ComObject which I cannot use for anything useful.

+1


a source to share


2 answers


I was able to replicate your original problem, but once I added the [Serializable] attribute to the class in the array list, I was able to see the objects as their correct type.

Here is some sample code showing a small working example.



public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
        this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
    }

    [Serializable]
    class DragClass
    {
        public string Prop1 { get; set; }
        public int Prop2 { get; set; }
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        System.Collections.ArrayList aDragClasses = new System.Collections.ArrayList();
        aDragClasses.Add(new DragClass() { Prop1 = "Test1", Prop2 = 2 });
        aDragClasses.Add(new DragClass() { Prop1 = "Test2", Prop2 = 3 });
        aDragClasses.Add(new DragClass() { Prop1 = "Test3", Prop2 = 4 });

        DoDragDrop(aDragClasses, DragDropEffects.Copy);
    }

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        foreach (var aData in (System.Collections.IEnumerable)e.Data.GetData(typeof(System.Collections.ArrayList)))
        {
          System.Diagnostics.Debug.WriteLine(((DragClass)aData).Prop1);
        }
    }



}

      

+3


a source


I think the problem is that you are using a list directly to pass data. I tried several ways to get it to fail, and figured out several ways that it won't work.

If you don't have the [Serializable] attribute in your custom classes, it won't work correctly, because that's how classes are marshaled between processes. Also, if I use List directly to pass data, I get a null reference exception.

If you are using a simple transport class to transfer data (and all types are serializable) then everything worked for me.



[Serializable]
class Test
{
    public string Name { get; set; }
    public string Description { get; set; }
}

[Serializable]
class Transport
{
    public Transport()
    {
        this.Items = new List<Test>();
    }
    public IList<Test> Items { get; private set; }
}

      

Then I can do it without issue and it works through instances ...

private void Form1_DragDrop(object sender, DragEventArgs e)
{
    foreach (var item in ((Transport)e.Data.GetData(typeof(Transport))).Items)
    {
        System.Diagnostics.Debug.WriteLine(item.Name + " " + item.Description);
    }
}

      

0


a source







All Articles