在 TreeView C# 中只查找子节点 [英] Find only child nodes in TreeView C#

查看:32
本文介绍了在 TreeView C# 中只查找子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下树

A
+-B
+-C 
| +-D
| +-E
+-F
  +-G
  +-H

我正在寻找B、D、E、G、H.排除有树的节点,只保留它们的子节点.

I am trying to find B, D, E, G, H. Exclude nodes that have tree and just keep their childs.

推荐答案

您可以在 TreeNode 列表中递归收集子项:

You can collect the children recursively in a List of TreeNodes:

 List<TreeNode> children = new List<TreeNode>();

 foreach(TreeNode node in  TV.Nodes) collectChildren(node);

 void collectChildren(TreeNode node)
 {
    if (node.Nodes.Count == 0) children.Add(node) 
    else  foreach(TreeNode n in node.Nodes) collectChildren(n);
 }

这篇关于在 TreeView C# 中只查找子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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