Treeview - 遍历所有节点,首先停止可见 [英] Treeview - iterate through all nodes, stop at first visible

查看:89
本文介绍了Treeview - 遍历所有节点,首先停止可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正处理一个心理障碍,这可能不会那么难。

我有一个函数应该在TreeView中获取最后一个可见节点。首先我想去第一个可见节点然后从那里开始第二个就像node.IsVisible()。



我尝试过:



I'm having a mental block right now, this can't be that hard.
I have a function which should get the last visible node in the TreeView. First I want to go to the first visible node and then second from there on go as long as node.IsVisible().

What I have tried:

public TreeNode GetLastVisibleNode()
    {
        var node = treeControl1.Nodes.Cast<TreeNode>().Where(x => x.IsVisible).FirstOrDefault();
        TreeNode retVal = node;
        while (node != null && node.IsVisible)
        {
            if (!node.IsSelected)
                retVal = node;
            node = node.NextVisibleNode;
        }
        return retVal;
    }





当我有第一个可见(HOORAY)时,我让它迭代到最后一个可见节点但我的方法第一个可见的是WRONG(OHHHHH!)

我注意到treeControl1.Nodes只给我父节点,但是我想要获得所有节点的第一个可见节点。

我也知道我可能需要一个递归方法,但正如我在开始时说的那样,我现在有一个心理障碍,并希望很快得到修复:(



I made it to iterate to the last visible node when I have the first visible (HOORAY) but my approach of getting the first visible is WRONG (OHHHHH!)
I noticed that treeControl1.Nodes only gives me the parent nodes but obvioulsy I want to get the first visible node of all nodes.
I also know that I probably need a recursive method but as I said in the beginning, I'm having a mental block right now and want to have this fixed quite soon :(

推荐答案

以下应该有效:



The following should work:

private static TreeNode FindFirstVisible(TreeNodeCollection nodes)
{
  foreach (TreeNode node in nodes)
  {
    if (node.IsVisible)
      return node;

    TreeNode first = FindFirstVisible(node.Nodes);
    if (first != null)
      return first;
  }

  return null;
}



要拨打电话,请按以下方式使用:




To call, use as follows:

TreeNode first = FindFirstVisible(tree.Nodes);



为了完整起见,您可能还想看看(这与你的要求略有不同):

TreeView.TopNode属性(System.Windows.Forms)| Microsoft Docs [ ^ ]



实际上,想一想,你甚至不需要深入挖树。我相信子节点是可见的,父节点必须是可见的。所以,我认为你必须有一个顶级节点可见,或者没有节点可见。因此,以下内容应该有效:




For completeness, you may also want to check out (which is slightly different from what you ask):
TreeView.TopNode Property (System.Windows.Forms) | Microsoft Docs[^]

Actually, thinking a bit longer, you shouldn't even need to dig into the tree. I believe for a child node to be visible the parent must be visible. So, I think you either must have a top level node visible, or no nodes are visible. So, the following should work:

private static TreeNode FindFirstVisible(TreeNodeCollection nodes) =>
  nodes.Cast<TreeNode>().FirstOrDefault(node => node.IsVisible);



根据后续信息,如果您担心滚动,以下方法将允许您选择最后一个部分或完全可见的节点。使用 NextVisibleNode 将不会考虑节点是否滚动超出范围。


Based on subsequent information, and if you are concerned about scrolling, the following methods will let you select the last partially or fully visible node. Using NextVisibleNode will not consider whether the node has scrolled out of range.

private TreeNode GetFirstVisibleNode(TreeView tree, bool includePartial)
{
  if (!includePartial)
    return tree.TopNode;

  Rectangle treeBounds = tree.ClientRectangle;
  return tree.Nodes.Cast<TreeNode>()
    .FirstOrDefault(node => node.IsVisible &&
      treeBounds.Contains(node.Bounds));
}

private TreeNode GetLastVisibleNode(TreeView tree, bool includePartial)
{
  TreeNode last = GetFirstVisibleNode(tree, includePartial);
  Rectangle treeBounds = tree.Bounds;

  for (TreeNode node = last; node != null; node = node.NextVisibleNode)
  {
    if (includePartial)
    {
      if (!treeBounds.IntersectsWith(node.Bounds))
        break;
    }
    else
    {
      if (!treeBounds.Contains(node.Bounds))
        break;
    }

    last = node;
  }

  return last;
}


public TreeNode GetLastVisibleNode()
        {
            return treeControl1.Nodes.Cast<TreeNode>().Select(GetLastVisibleNode).
                            LastOrDefault(first => first != null);
        }

TreeNode GetLastVisibleNode(TreeNode parentNode) =>
        parentNode.IsVisible
             ? parentNode
             : parentNode.Nodes.Cast<TreeNode>().Select(GetLastVisibleNode).
                      LastOrDefault(childFirstNode => childFirstNode != null);



有人回答了我问过的同样问题这里


我在评论中告诉我,TreeView.TopNode为我提供了第一个可见节点。



从那里开始,我只是迭代遍历列表,只要node.IsVisible和node!= null。

I was told in the comments that TreeView.TopNode gets me the first visible node.

From there on I just iterated through the list as long as node.IsVisible and node != null.
public TreeNode GetLastVisibleNode()
        {
            TreeNode node = treeControl1.TopNode;
            TreeNode retVal;
            do
            {
                retVal = node;
                node = node.NextVisibleNode;
            } while (node != null && node.IsVisible);

            return retVal;
        }


这篇关于Treeview - 遍历所有节点,首先停止可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆