.NET TreeView 在尝试检查父节点时导致应用程序崩溃

发布于 2024-09-02 21:06:36 字数 462 浏览 0 评论 0原文

我有一个带有复选框的 TreeView,当用户检查子节点时,我想在树上检查每个父节点。但是,由于某种原因,每当我触摸父节点时,我的应用程序就会崩溃。对于树中的节点,我扩展了 TreeNode 来创建自己的对象,其中包含一些我需要存储在其中的数据,但在检查/取消检查时我仍然将它们引用为 TreeNodes。我的代码如下所示:

//checkBox checked event handler
    if (node.Parent != null)
    {
        checkAllParents(node.Parent);
    }
//

private void checkAllParents(TreeNode node)
{
    node.Checked = true;
    if (node.Parent != null)
    {
       checkAllParents(node.Parent);
    }
}

I have a TreeView with check boxes, and when a user checks a child node, I want to go up the tree and check each parent. However, for some reason my application blows up whenever I touch the parent node. For the nodes in my tree, I've extended TreeNode to create my own objects with some data that I need to store in them, but I still reference them as TreeNodes when checking/unchecking. My code looks like this:

//checkBox checked event handler
    if (node.Parent != null)
    {
        checkAllParents(node.Parent);
    }
//

private void checkAllParents(TreeNode node)
{
    node.Checked = true;
    if (node.Parent != null)
    {
       checkAllParents(node.Parent);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2024-09-09 21:06:36

好吧,想通了。它不是循环引用,但本质上肯定是循环引用。这对我来说是一个很大的愚蠢错误。在​​事件处理程序中,我也使用递归来检查树的向下...我不久前实现了这个,但并没有真正考虑它,所以当我添加另一块时递归检查树我最终在递归函数和事件处理程序之间出现了无限循环(每次递归函数检查节点之一时都会调用该循环......After_checked 事件)。呸。

Ok, figured it out. It wasn't a circular reference but it was most certainly circular in nature. This was a big dumb mistake on my part..in the event handler I was using recursion to check DOWN the tree as well...I implemented this a while ago and didn't really think about it, so when I added another piece of recursion to check up the tree I ended up with an infinite loop between the recursion function and the event handler (which was getting called every time the recursive function checked one of the nodes..the After_checked event). Bah.

灼疼热情 2024-09-09 21:06:36

如果你确实有一个循环引用,那么你会遇到堆栈溢出,这是无法捕获的。尝试修改您的代码以消除递归:

// checkBox checked event handler
checkAllParents(node);

private void checkAllParents(TreeNode node)
{
  var parent = node.Parent;
  while (parent != null) {
  {
    parent.Checked = true;
    parent = parent.Parent;
  }
}

如果应用程序进入无限循环,那么您确实有一个循环引用。您可以进一步修改代码以捕获循环引用:

private void checkAllParents(TreeNode node)
{
  var parent = node.Parent;
  var visitedNodes = new List<TreeNode>();
  while (parent != null)
  {
    if (visitedNodes.Contains(parent))
      throw new InvalidOperationException("Circular reference!");
    visitedNodes.Add(parent);
    parent.Checked = true;
    parent = parent.Parent;
  }
}

当然,这不是处理循环引用的正确位置。但如果有帮助的话..

If you really have a circular reference then you would get a stack overflow, which cannot be caught. Try to modify your code to get rid of the recursion:

// checkBox checked event handler
checkAllParents(node);

private void checkAllParents(TreeNode node)
{
  var parent = node.Parent;
  while (parent != null) {
  {
    parent.Checked = true;
    parent = parent.Parent;
  }
}

If app enters an infinite loop then you do have a circular reference. You could further modify the code to catch circular references:

private void checkAllParents(TreeNode node)
{
  var parent = node.Parent;
  var visitedNodes = new List<TreeNode>();
  while (parent != null)
  {
    if (visitedNodes.Contains(parent))
      throw new InvalidOperationException("Circular reference!");
    visitedNodes.Add(parent);
    parent.Checked = true;
    parent = parent.Parent;
  }
}

Of course this is not the right place to handle circular references. But if it helps..

旧情勿念 2024-09-09 21:06:36

肯定是无限递归导致堆栈崩溃(堆栈溢出也不少)。 node.Parent 决不能计算为 null。它不会被 try catch 捕获,因为您的代码没有抛出异常,您只是导致堆栈溢出,并且抛出异常的是 .net 框架。每次设置 node.Checked = true; 时,您还将多次触发递归。您将再次触发事件处理程序,但您还再次调用检查所有父级,我将删除递归调用以检查所有父级,只需将节点设置为选中,事件处理程序将为您执行递归。

It must be a infinite recursion that is blowing the stack (a stack overflow no less). node.Parent must never be evaluating to null. It won't be caught in a try catch because your code isn't throwing an exception, you are just causing a stack overflow and it is the .net frameworkthat throw the exception. You will also be triggering the recursion multiple times, every time you set node.Checked = true; you will fire of the event handler again but you also call check all parents again, i would remove the recursive call to check all parents and just set the node to checked, the event handler will do the recursion for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文