C# 从列表框拖放到树视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/495666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:38:50  来源:igfitidea点击:

C# Drag & drop from listbox to treeview

c#.netwinformsdrag-and-drop

提问by Gerbrand

I have a winform with a listbox and a treeview.

我有一个带有列表框和树视图的 winform。

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.

如果有人在 C# 中有一个很好的例子,那就太好了。

采纳答案by BFree

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.

基本上,我有一个表单,左侧有一个列表框,右侧有一个树视图。然后我在上面放了一个按钮。单击该按钮时,它只是将接下来十天的日期放入列表框中。它还使用 2 个父节点和两个子节点填充 TreeView。然后,您只需要处理所有后续的拖/放事件即可使其工作。

 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);
        }


    }

回答by arul

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

您想使用 GetItemAt(Point point) 函数将 X、Y 位置转换为列表视图项。

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

这是关于它的相当不错的文章:使用 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.

要在拖动时使被拖动的项目可见,您需要使用 COM ImageList,下面的文章使用 ImageLists 自定义拖放图像对此进行了很好的描述。