代码之家  ›  专栏  ›  技术社区  ›  Julien Poulin

执行拖放操作时发生异常

  •  7
  • Julien Poulin  · 技术社区  · 15 年前

    我有一个WinForms应用程序,我在两个TreeView之间进行拖放操作。

    例外情况到哪里去了?

    下面是一些描述问题的代码:

    private TreeView tvLeft;
    private TreeView tvRight;
    private Dictionary<string, int> dico = new Dictionary<string, int>();
    
    void tvLeft_DragDrop(object sender, DragEventArgs e) {
    
      if (e.Data.GetDataPresent(typeof(TreeNode))) {
    
        var tnSource = (TreeNode) e.Data.GetData(typeof(TreeNode));
        var tnDestination = tvLeft.GetNodeAt(tvLeft.PointToClient(new Point(e.X, e.Y)));
    
        // if I drag-drop the same node twice, there sould be an Exception
        // since the key is already in the dictionary...
        // ...but I get no Exception in the UI, the Application.ThreadException
        // or Appomain.CurrentDomain.UnhandledException handlers
        dico.Add(tnSource.Name, (new Random()).Next());
    
      }
    
    }
    
    2 回复  |  直到 15 年前
        1
  •  12
  •   Viper    15 年前

    我在互联网上找到了这样的解释:

    即使在同一应用程序中进行拖放,也可以通过标准的OLE拖放机制来处理拖放。从OLE的角度来看,它处理两个应用程序,源程序和目标程序,并将它们适当地解耦。由于OLE的存在时间远长于.NET,因此OLE没有.NET异常的概念,因此无法将异常从目标通信回源。即使可以,源代码为什么要关心目标无法执行删除?

    看见 here 问题后的第一个答案。

        2
  •  0
  •   Simon P Stevens    15 年前

    异常可能发生在某个背景线程上。您需要为AppDomain.CurrentDomain.UnhandledException或Application.ThreadException事件创建处理程序。

    看见 here