11

I have a winform with a listbox and a treeview.

Once my listbox is filled with items, I want to drag them (multiple or single) from the listbox and drop them in a node in the treeview.

If somebody has a good example in C# that would be great.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gerbrand
  • 5,274
  • 4
  • 29
  • 34
  • 1
    Can you edit your post and tell us exactly what part of this you're having a problem with? Folks here tend not to respond well to "ples send the codz" type questions – Binary Worrier Jan 30 '09 at 14:36

2 Answers2

22

It's been a while since I've messed with Drag/Drop so I figured I'll write a quick sample.

Basically, I have a form, with a listbox on the left, and a treeview on the right. Then I put a button on top. When the button is clicked, it just puts the date of the next ten days into the list box. It also populates the TreeView with 2 parents nodes and two child nodes. Then, you just have to handle all the subsequent drag/drop events to make it work.

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.treeView1.AllowDrop = true;
            this.listBox1.AllowDrop = true;
            this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
            this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);

            this.treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
            this.treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.PopulateListBox();
            this.PopulateTreeView();
        }

        private void PopulateListBox()
        {
            for (int i = 0; i <= 10; i++)
            {
                this.listBox1.Items.Add(DateTime.Now.AddDays(i));
            }
        }

        private void PopulateTreeView()
        {
            for (int i = 1; i <= 2; i++)
            {
                TreeNode node = new TreeNode("Node" + i);
                for (int j = 1; j <= 2; j++)
                {
                    node.Nodes.Add("SubNode" + j);
                }
                this.treeView1.Nodes.Add(node);
            }
        }

        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {

            TreeNode nodeToDropIn = this.treeView1.GetNodeAt(this.treeView1.PointToClient(new Point(e.X, e.Y)));
            if (nodeToDropIn == null) { return; }
            if(nodeToDropIn.Level > 0)
            {
                nodeToDropIn = nodeToDropIn.Parent;
            }

            object data = e.Data.GetData(typeof(DateTime));
            if (data == null) { return; }
            nodeToDropIn.Nodes.Add(data.ToString());
            this.listBox1.Items.Remove(data);
        }

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

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

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
        }


    }
BFree
  • 102,548
  • 21
  • 159
  • 201
  • Thank you so much for this. I just needed a code example to confirm that I'm not getting drag and drop "effects" in Mono (at least on OSX). Your great and COMPLETE example saved me a ton of work. – Dan Rosenstark Jul 31 '09 at 08:59
  • Hi I tried your code but the item being dragged is always being added to the root (first) node of the tree, for some reason. The `nodeToDropIn.Text` returns the correct node name, but item gets added to the parent node. what might be the problem ? – mrid Dec 22 '19 at 16:39
2

You want to use the GetItemAt(Point point) function to translate X,Y location to the listview item.

Here's quite good article about it: Drag and Drop Using C#.

To make the item being dragged visible while dragging, you need to use COM ImageList, which is well described in the following article Custom Drag-Drop Images Using ImageLists.

arul
  • 13,998
  • 1
  • 57
  • 77