简体   繁体   English

TreeView会触发DragEnter,DragOver和DragLeave事件,但不会触发DragDrop

[英]TreeView fires DragEnter, DragOver and DragLeave events, but won't fire DragDrop

Hail Stack! 冰雹堆栈!

I'm having a hard time trying to figure out why my treeview (or any other component, even the form itself) won't fire the event DragDrop. 我很难弄清楚为什么我的树视图(或任何其他组件,甚至表单本身)不会触发事件DragDrop。

I've settled my form like this: 我已经按照以下方式结算了我的表格:
A Form with a Panel inside. 内部带有面板表单
The Panel have a TreeView , and this TreeView ( MyTree ) has the following code: 面板具有TreeView ,并且此TreeView( MyTree )具有以下代码:

MyTree.AllowDrop = true;
MyTree.DragDrop += new System.Windows.Forms.DragEventHandler(onDragDrop);
MyTree.DragEnter += new System.Windows.Forms.DragEventHandler(onDragEnter);
MyTree.DragLeave += new System.EventHandler(onDragLeave);
MyTree.DragOver += new System.Windows.Forms.DragEventHandler(onDragOver);

The handlers looks like this: 处理程序如下所示:

private void onDragEnter(object sender, DragEventArgs e)
{
    Console.WriteLine(" === DragEnter === ");
}

private void onDragLeave(object sender, EventArgs e)
{
    Console.WriteLine(" === DragLeave === ");
}

private void onDragOver(object sender, DragEventArgs e)
{
    Console.WriteLine(" === DragOver === ");
}

private void onDragDrop(object sender, DragEventArgs e)
{
    Console.WriteLine(" === DragDrop === ");
}

When I test my app, dragging a *.txt file (or anything) the output are something like: 当我测试我的应用程序时,拖动一个* .txt文件(或其他任何内容),输出类似于:

=== DragEnter ===
=== DragOver ===
=== DragOver ===
...
=== DragOver ===
=== DragLeave ===

The last line ( === DragLeave === ) wasn't meant to be a leave event. 最后一行(=== DragLeave ===)并非要离开事件。
In fact, this line is printed when I release the mouse button over my TreeView. 实际上,当我在TreeView上释放鼠标按钮时,将打印此行。

I'm I doing something terribly wrong? 我做错了什么吗?

this is what I use for drag and drop to a treeview for files: 这就是我用来拖放到树形视图中用于文件的内容:

public class DragDropManager
{
    private UserControl _parent;

    private AddFilesEventHandler OnAddFiles;   

    public DragDropManager()
    {
    }

    public UserControl Parent
    {
        set
        {
            _parent = value;    

            if ( ! ( _parent is IDropFileTarget ) )
            {
                throw new Exception("DragDropManager: Parent usercontrol does not implement IDropFileTarget interface");
            }

            OnAddFiles = new AddFilesEventHandler(((IDropFileTarget)_parent).AddFiles);
            _parent.AllowDrop = true;
            _parent.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
            _parent.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
        }
    }

    /// <summary>
    /// Handle parent form DragEnter event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
        string[] formats = e.Data.GetFormats(true);

        //e.Effect = DragDropEffects.All;

        for (int formatIndex = 0; formatIndex < formats.Length; formatIndex++)
        {
            switch (formats[formatIndex])
            {
                case Consts.DragDropFormats.FileDrop:
                    e.Effect = DragDropEffects.Copy;
                    break;
                case Consts.DragDropFormats.Text:
                    e.Effect = DragDropEffects.Move;
                    break;
                case Consts.DragDropFormats.UniformResourceLocator:
                    e.Effect = DragDropEffects.Link;
                    break;
            }
        }
    }

    /// <summary>
    /// Handle parent form DragDrop event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
        try
        {
            string[] formats = e.Data.GetFormats(true);
            string[] values = new string[1];
            string url = string.Empty;

            for (int formatIndex = 0; formatIndex < formats.Length; formatIndex++)
            {
                switch (formats[formatIndex])
                {
                    case Consts.DragDropFormats.FileDrop:
                        Array itemList = (Array)e.Data.GetData(Consts.DragDropFormats.FileDrop);

                        if (itemList != null)
                        {
                            _parent.BeginInvoke(OnAddFiles, new Object[] { itemList });
                            _parent.Focus();
                        }
                        break;
                    case Consts.DragDropFormats.Text:
                    case Consts.DragDropFormats.UniformResourceLocator:
                        values[0] = e.Data.GetData(Consts.DragDropFormats.Text).ToString();
                        _parent.BeginInvoke(OnAddFiles, new Object[] { values });
                        _parent.Focus();
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine("Error in DragDropManager.OnDragDrop function: " + ex.Message);
        }
    }

}

you can use it as follows: 您可以按以下方式使用它:

DragDropManager dragDropManager = new DragDropManager();
dragDropManager.Parent = this;

and need to implement this on the UserControl 并且需要在UserControl上实现

public interface IDropFileTarget
{
    void AddFiles(Array Files);
}

您需要将DragOver e.Effect设置为None以告诉系统您愿意e.Effect

You need to make sure the dragged item actually has some data in it. 您需要确保所拖动的项目中确实有一些数据。 Get a string array of what's present using 使用以下命令获取当前内容的字符串数组

e.Data.GetFormats()

Use each element of the string array 'fmt' as an argument to GetData 使用字符串数组'fmt'的每个元素作为GetData的参数

e.Data.GetData(fmt)

If they're all null, it's not going to fire the DragDrop event and no amount of setting e.Effect is going to change that. 如果它们全为空,则不会触发DragDrop事件,也不会设置e.Effect来改变它。 If you determine this to be the cause, you can be more specific in searching for the root cause. 如果确定这是原因,则可以更具体地寻找根本原因。 (in my case, Internet Explorer) (就我而言,是Internet Explorer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM