.Net常用技巧_树控件工具类
using System; using System.Collections.Generic; using System.Text; using System.Collections; using DevComponents.AdvTree; namespace MyUtility { /// <summary> /// 树控件工具类 /// </summary> public class TreeTool { ArrayList selectArray = new ArrayList(); List<Node> list = new List<Node>(); /// <summary> /// 选中父节点,其所有子节点也选中 /// </summary> /// <param name="parentNode"></param> public ArrayList SetChildChecked(Node parentNode,ArrayList list) { for(int i = 0;i<parentNode.Nodes.Count;i++){ Node node = parentNode.Nodes[i]; node.Checked = true; //list.Add(node); if (node.HasChildNodes) { list = SetChildChecked(node, list); } else { list.Add(node); } } return list; } /// <summary> /// 选中子节点,其父节点也选中 /// </summary> /// <param name="childNode"></param> public void SetParentChecked(Node childNode) { if (childNode.Parent != null) { childNode.Parent.Checked = true; SetParentChecked(childNode.Parent); } } /// <summary> /// 取消子节点,其父节点也取消 /// </summary> /// <param name="childNode"></param> public void SetParentUnchecked(Node childNode) { if (childNode.Parent != null) { childNode.Parent.Checked = false; SetParentUnchecked(childNode.Parent); } } /// <summary> /// 取消父节点,其所有子节点也取消 /// </summary> /// <param name="parentNode"></param> public ArrayList CancelChildChecked(Node parentNode, ArrayList list) { for(int i=0;i<parentNode.Nodes.Count;i++){ Node node = parentNode.Nodes[i]; node.Checked = false; list.Remove(node); if (node.HasChildNodes) { list = CancelChildChecked(node, list); } } return list; } /// <summary> /// 根据名称模糊查找节点 /// </summary> /// <param name="node">根节点</param> /// <param name="text">名称</param> /// /// <param name="text">查找类型,0为查找父节点,1为查找子节点</param> /// <returns></returns> public List<Node> searchNode(Node node, String text,int flag) { if (flag == 0&&node.Text.Contains(text)) { list.Add(node); } else if (!node.HasChildNodes && node.Text.Contains(text)) { list.Add(node); } if (node.HasChildNodes) { for (int i = 0; i < node.Nodes.Count; i++) { list = searchNode(node.Nodes[i], text,flag); } } return list; } /// <summary> /// 根据值精确查找节点 /// </summary> /// <param name="node"></param> /// <param name="text"></param> /// <returns></returns> public List<Node> searchNode_value(Node node, String value) { if (node.Tag.ToString().Equals(value) && !node.HasChildNodes) { list.Add(node); } else if (node.HasChildNodes) { for (int i = 0; i < node.Nodes.Count; i++) { list = searchNode_value(node.Nodes[i], value); } } return list; } /// <summary> /// 遍历已选择节点 /// </summary> /// <param name="node"></param> /// <param name="perList"></param> /// <returns></returns> public ArrayList visitSelectNode(Node node) { if (!node.HasChildNodes) { if (node.Checked == true) { selectArray.Add(node); } else {//取消父节点 SetParentUnchecked(node); } } else { node.Checked = true; for (int i = 0; i < node.Nodes.Count; i++) { selectArray = visitSelectNode(node.Nodes[i]); } } return selectArray; } /// <summary> /// 遍历节点,并且选择 /// </summary> /// <param name="node"></param> /// <param name="perList"></param> /// <returns></returns> public void visitNode(Node node, ArrayList list) { if (!node.HasChildNodes) { if (list.Count != 0) { foreach (String id in list) { if (node.Checked == true) { node.Checked = false; } if (node.Tag.ToString().Equals(id)) { node.Checked = true; break; } } } else { node.Checked = false; } } else { for (int i = 0; i < node.Nodes.Count; i++) { visitNode(node.Nodes[i], list); } } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。