示例:获取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;
}

疑问:能不能使用一个方法既可以实现循环遍历,又可以获得返回值???

最新文章

  1. IIS上部署Net.Core
  2. 【Beta版本】冲刺-Day3
  3. Cucumber命令行接口
  4. 升级WebService图形服务,将K10.2和K10.3写到一个类库,所有服务放在一个类库
  5. 【WEB API项目实战干货系列】- 接口文档与在线测试(二)
  6. wflag
  7. Android Handler简单示例
  8. 销售 &gt;&gt; 当今社会生产力最大的源泉为 &gt;&gt;自助服务 与推销员随之消失
  9. 我认为测试应该掌握的SQL语句
  10. AdminCMD
  11. Oracle SQL Lesson (1) - 使用SQL Select语句获取数据
  12. SOCKET是什么
  13. opencv基础教程
  14. Sql中获取表结构(字段名称,类型,长度,说明)
  15. Json.net 反序列化 部分对象
  16. 输入一个数,求1到他 的和(for循环)
  17. PRD文档怎么写
  18. 使用基于 PHP 的开源软件 YOURLS 搭建短链接地址服务
  19. 熟悉一下oncontextmenu事件的知识
  20. Django分页类的封装

热门文章

  1. ListView中CheckBox使用问题
  2. 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings
  3. GitHub 寻宝指南
  4. Java并发基础:了解无锁CAS就从源码分析
  5. Laravel开发:Laravel核心——Ioc服务容器源码解析(服务器绑定)
  6. 旋转卡壳求两个凸包最近距离poj3608
  7. SQLServer2008 统计表占用空间
  8. Pairs of Integers
  9. dubbo开发中使用到的一些服务配置方式
  10. amoeba安装与简单使用(一)