problem getting data from drag drop

T

Tarscher

Hi all,

I have a 2 classes (A and B) inheriting from 1 base class (parent). I
populate a listbox with those 2 classes (A and B).

I want to implement some drag and drop ordering code on the listbox.
To get the data out of the dragged item I try:
Parent myParent = (Parent)e.Data.GetData(typeof(Parent));

The myParent is always null. Debugging tells me that the Data is
filled with the correct data (either A or B). Someone has a clue what
I'm doing wrong?

Thanks
Stijn
 
C

ClayB

Try using a custom string to name the format type. Set the MouseDown
code below. Here is a complete little program that seems to work ok
for me. Just drop 2 listboxes on the form and drab from listBox1 to
listBox2.
{
BindingList<Parent> list1;
BindingList<Parent> list2;
private void Form1_Load(object sender, EventArgs e)
{
this.list1 = new BindingList<Parent>();
list1.Add(new AParent("a1", "p1"));
list1.Add(new BParent("b1", "p2"));
list1.Add(new AParent("a2", "p3"));
list1.Add(new BParent("b2", "p4"));
this.listBox1.DataSource = list1;

this.list2 = new BindingList<Parent>();
list2.Add(new AParent("a20", "p20"));
list2.Add(new BParent("b20", "p21"));
list2.Add(new AParent("a21", "p22"));
list2.Add(new BParent("b21", "p23"));
this.listBox2.DataSource = list2;

listBox2.AllowDrop = true;

this.listBox1.MouseDown += new
MouseEventHandler(listBox1_MouseDown);
this.listBox2.DragEnter += new
DragEventHandler(listBox2_DragEnter);
this.listBox2.DragDrop += new
DragEventHandler(listBox2_DragDrop);
}

void listBox2_DragDrop(object sender, DragEventArgs e)
{
Parent p = e.Data.GetData("custom") as Parent;
if (p != null)
{
list2.Add(p);
}
else
e.Effect = DragDropEffects.None;
}

void listBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

void listBox1_MouseDown(object sender, MouseEventArgs e)
{
Parent p = this.listBox1.SelectedItem as Parent;
DataObject data = new DataObject();
data.SetData("custom", p);
DragDropEffects eff = this.listBox1.DoDragDrop(data,
DragDropEffects.Move);
if (eff == DragDropEffects.Move)
this.list1.Remove(p);
}
}

public class Parent
{
public Parent(string name)
{
this.name = name;
}

private string name;

public string Name
{
get { return name; }
set { name = value; }
}
}

public class AParent : Parent
{
public AParent(string aname, string name)
: base(name)
{
this.aname = aname;
}

private string aname;

public string AName
{
get { return aname; }
set { aname = value; }
}

public override string ToString()
{
return string.Format("A={0} Parent={1}", aname, Name);
}
}

public class BParent : Parent
{
public BParent(string bname, string name)
: base(name)
{
this.bname = bname;
}

private string bname;

public string BName
{
get { return bname; }
set { bname = value; }
}

public override string ToString()
{
return string.Format("B={0} Parent={1}", bname, Name);
}
}
=================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top