Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

problem getting data from drag drop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tarscher - 21 Feb 2007 16:27 GMT
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
ClayB - 22 Feb 2007 08:52 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.