为什么在节点拖出TreeView时不会触发DragLeave事件?

时间:2014-02-26 11:38:28

标签: c# winforms drag-and-drop treeview nodes

正如我在其他问题中所读到的那样,大多数人都会问为什么它会意外发射,但对我来说它根本不会发射。

using System;
using System.Windows.Forms;

namespace TestDrag
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ItemDrag += treeView1_ItemDrag;
            treeView1.DragLeave += treeView1_DragLeave;

            groupBox1.AllowDrop = true;
            groupBox1.DragEnter += groupBox1_DragEnter;
            groupBox1.DragDrop += groupBox1_DragDrop;

            treeView1.Nodes.Add("asd");
            treeView1.Nodes.Add("dsa");
        }

        void groupBox1_DragDrop(object sender, DragEventArgs e)
        {
            throw new NotImplementedException();
        }

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

        void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            treeView1.DoDragDrop(e.Item, DragDropEffects.Copy);
        }

        void treeView1_DragLeave(object sender, EventArgs e)
        {
            MessageBox.Show(this, "This message never shows when node dragged out of treeView1", "This is bad", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

我希望在项目被拖出后隐藏TreeView,但此事件永远不会发生> _< 可能是什么原因以及如何修复或绕过这个?

1 个答案:

答案 0 :(得分:1)

你必须AllowDrop = True;