【问题标题】:c# drag drop DataObjectc# 拖放DataObject
【发布时间】:2011-05-20 15:13:03
【问题描述】:

我正在使用 C# 2.0,并且我创建了一个资源管理器树,我可以将信息从该树拖到 Windows 窗体中。现在,当我从我使用的树视图中拖动时,它执行DoDragDrop(selectedpath, DragDropEffects.Copy);

当我在主窗体中捕获事件时,它被列为字符串。我能够完成这项工作,但我希望它的执行方式与我从 Windows 资源管理器窗口中拖动文件的方式相同,如下所示

   Array name = (Array)e.Data.GetData(DataFormats.FileDrop);
        // Ensure that the list item index is contained in the data.
        if (e.Data.GetDataPresent(typeof(System.String)))
        {

            Object item = (object)e.Data.GetData(typeof(System.String));

            // Perform drag-and-drop, depending upon the effect.
            if (e.Effect == DragDropEffects.Copy ||
                e.Effect == DragDropEffects.Move)
            {

                //// Insert the item.
                //if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
                //    ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item);
                //else
                //    ListDragTarget.Items.Add(item);

            }
        }

我尝试过 DoDragDrop(new DataObject(selectedfile),DragDropEffects.Copy) 但这不起作用。

【问题讨论】:

  • 至少告诉那个人你为什么拒绝他的问题!
  • 谢谢。我已经搜索了答案,但似乎没有找到。
  • 我认为没有理由对这个问题投反对票。 +1

标签: c# .net winforms drag-and-drop


【解决方案1】:

DoDragDropDragDropEffects.Copy 不会对您的驱动器执行操作,除非您告诉他们这样做。 DragDropEffects.Copy 所做的实际上是复制程序中的对象,而不是文件本身。

请参阅DragDropEffects 上的文档。

您需要管理OnDragDrop 事件并使用File.Copy 之类的复制功能

【讨论】:

  • 我就是这么做的。我通过执行以下操作最终找到了使其工作的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多