Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TreeView发射DragEnter、DragOver和DragLeave事件,但赢得';t型火吸管_C#_.net_Drag And Drop - Fatal编程技术网

C# TreeView发射DragEnter、DragOver和DragLeave事件,但赢得';t型火吸管

C# TreeView发射DragEnter、DragOver和DragLeave事件,但赢得';t型火吸管,c#,.net,drag-and-drop,C#,.net,Drag And Drop,冰雹堆 我很难弄明白为什么我的treeview(或任何其他组件,甚至表单本身)不会触发事件DragDrop 我的表格是这样定的: 一张表格内有面板。 面板有一个树状视图,此树状视图(MyTree)具有以下代码: MyTree.AllowDrop = true; MyTree.DragDrop += new System.Windows.Forms.DragEventHandler(onDragDrop); MyTree.DragEnter += new System.Windows.Forms.

冰雹堆

我很难弄明白为什么我的treeview(或任何其他组件,甚至表单本身)不会触发事件DragDrop

我的表格是这样定的:
一张表格内有面板
面板有一个树状视图,此树状视图(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);
处理程序如下所示:

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 === ");
}
当我测试我的应用程序时,拖动一个*.txt文件(或任何东西),输出如下:

=== DragEnter ===
=== DragOver ===
=== DragOver ===
...
=== DragOver ===
=== DragLeave ===
最后一行(==DragLeave===)不是休假事件。
事实上,当我在我的树视图上释放鼠标按钮时,这一行就被打印出来了


我做错了什么?

您需要将
DragOver
中的
e.Effect
设置为
None
以外的其他值,以告诉系统您愿意被删除。

这是我用于拖放到树视图中查看文件的方法:

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

}
需要在UserControl上实现这一点

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

您需要确保拖动的项中确实包含一些数据。使用以下命令获取当前内容的字符串数组

e.Data.GetFormats()
使用字符串数组“fmt”的每个元素作为GetData的参数

e.Data.GetData(fmt)

如果它们都为null,则不会触发DragDrop事件,也不会设置任何数量的e。Effect会改变这一点。如果确定这是原因,则可以更具体地搜索根本原因。(就我而言,Internet Explorer)

谢谢你,@SLaks。改变e.对DragEnter或DragOver的效果就像一个符咒。
e.Data.GetData(fmt)