.Net常用技巧_樹控制項工具類

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   

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);                }            }        }            }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.