循环递归+返回值(TreeView示例)
2024-09-03 07:39:17
示例:获取TreeView的所有Node,保存到List<TreeNode>,封装到通用工具类:
方法一:使用static方法、属性---调用前清空static类型的List
public static List<TreeNode> allNodes = null;
public static void GetTreeViewAllNodes(TreeNodeCollection Nodes)
{
foreach (TreeNode node in Nodes)
{
allNodes.Add(node);
if (node.ChildNodes.Count > )
{
GetTreeViewAllNodes(node.ChildNodes);
}
}
}
public static List<TreeNode> GetNodes(TreeView tv)
{
GetTreeViewAllNodes(tv.Nodes);
return allNodes;
}
注意:如果使用static类型的List,每次调用该通用工具类之前需要清空List<TreeNode>,否则会出现叠加。在构造函数中清空:
public Tools()
{
allNodes.Clear();
}
方法二、使用内部方法、属性---直接调用
public List<TreeNode> allNodes = new List<TreeNode>();
public void GetTreeViewAllNodes(TreeNodeCollection Nodes)
{
foreach (TreeNode node in Nodes)
{
allNodes.Add(node);
if (node.ChildNodes.Count > )
{
GetTreeViewAllNodes(node.ChildNodes);
}
}
}
public List<TreeNode> GetNodes(TreeView tv)
{
GetTreeViewAllNodes(tv.Nodes);
return allNodes;
}
疑问:能不能使用一个方法既可以实现循环遍历,又可以获得返回值???
最新文章
- IIS上部署Net.Core
- 【Beta版本】冲刺-Day3
- Cucumber命令行接口
- 升级WebService图形服务,将K10.2和K10.3写到一个类库,所有服务放在一个类库
- 【WEB API项目实战干货系列】- 接口文档与在线测试(二)
- wflag
- Android Handler简单示例
- 销售 >;>; 当今社会生产力最大的源泉为 >;>;自助服务 与推销员随之消失
- 我认为测试应该掌握的SQL语句
- AdminCMD
- Oracle SQL Lesson (1) - 使用SQL Select语句获取数据
- SOCKET是什么
- opencv基础教程
- Sql中获取表结构(字段名称,类型,长度,说明)
- Json.net 反序列化 部分对象
- 输入一个数,求1到他 的和(for循环)
- PRD文档怎么写
- 使用基于 PHP 的开源软件 YOURLS 搭建短链接地址服务
- 熟悉一下oncontextmenu事件的知识
- Django分页类的封装