C# Tree的例子1

 

private void LoadTreRoleMenu()

        {

            arr = new ArrayList();

            brr = new ArrayList();

            FirstNodeName = this.WinControlUtility.GetMessageText("ManageAccessRightsFirstNodeName");

    //FirstNodeName为“父功能菜单”

            id = "";

            this.TreRoleMenu.Nodes.Clear();    从集合中删除所有树节点

            this.TreRoleMenu.BeginUpdate();    禁用任何树视图重绘

            this.TreRoleMenu.Nodes.Add(FirstNodeName);  将具有指定标签文本的新树节点添加到当前树节点集合的末尾

            this.SetNode(TreRoleMenu.Nodes[0]);  

            this.TreRoleMenu.EndUpdate();      启用树视图的重绘

        }

 

        private void SetNode(TreeNode treeNode)     TreeView 的节点(类)

        {

            AccessRight art = new AccessRight();

            TreeNodeCollection treeNodeCollection = treeNode.Nodes;    TreeNode 对象的集合

            TreeNode tempTreeNode;

            DataRowCollection dataRowcollection = art.GetNames(id,treeNode.Text, FirstNodeName); DataTable 的行的集合

            foreach (DataRow row in dataRowcollection)

            {

                string tempName = row[1].ToString();

                id= row[0].ToString();

                arr.Add(id);

                brr.Add(tempName);

               

                tempTreeNode = new TreeNode();

                tempTreeNode.Text = tempName;

 

                treeNodeCollection.Add(tempTreeNode);

             }

         }

 

 

 

        /// <summary>

        /// 加载权限树

        /// </summary>

        private void LoadTreRoleMenu()

        {

            try

            {

                arr = new ArrayList();

                brr = new ArrayList();

                FirstNodeName = this.WinControlUtility.GetMessageText("ManageAccessRightsFirstNodeName");

                id = "";

                this.TreRoleMenu.Nodes.Clear();   从集合中删除所有树节点

                this.TreRoleMenu.BeginUpdate();    禁用任何树视图重绘

                this.TreRoleMenu.Nodes.Add(FirstNodeName);   将具有指定标签文本的新树节点添加到当前树节点集合的末尾

                this.SetNode(TreRoleMenu.Nodes[0]);

                this.TreRoleMenu.EndUpdate();   启用树视图的重绘

 

                //this.colorshow();

                //TreRoleMenu_AfterCheck(new object(), new TreeViewEventArgs(new TreeNode()));

                //TreRoleMenu.Nodes[0].Checked = true;

            }

            catch (Exception ex)

            {

                throw new ApplicationException(ex.Message);

            }

 

        }

 

        /// <summary>

        /// 添加根节点的子节点

        /// </summary>

        /// <param ></param>

        private void SetNode(TreeNode treeNode)

        {

            try

            {

                AccessRight art = new AccessRight();

                TreeNodeCollection treeNodeCollection = treeNode.Nodes;

                TreeNode tempTreeNode;

 

                DataRowCollection dataRowcollection = art.GetNames(id, treeNode.Text, FirstNodeName);

 

                foreach (DataRow row in dataRowcollection)

                {

                    id = row[0].ToString();

                    string tempName = row[1].ToString();

                    arr.Add(id);

                    brr.Add(tempName);

 

                    tempTreeNode = new TreeNode();

                    tempTreeNode.Text = tempName;

                    tempTreeNode.Name = id;

 

                    treeNodeCollection.Add(tempTreeNode);  /把新节点加入TreRoleMenu中

 

                    SetNode(id, tempTreeNode);

 

                    TreRoleMenu.ExpandAll();  展开所有节点

 

                }

            }

            catch (Exception ex)

            {

                throw new ApplicationException(ex.Message);

            }

 

        }

 

        /// <summary>

        /// 改变子节点的选中状态

        /// </summary>

        /// <param ></param>

        /// <param ></param>

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)

        {

            try

            {

                treeNode.ForeColor = Color.Black;

                foreach (TreeNode node in treeNode.Nodes)

                {

                    node.Checked = nodeChecked;

                    if (node.Nodes.Count > 0)

                    {

                        // If the current node has child nodes, call the CheckAllChildsNodes method recursively.

                        this.CheckAllChildNodes(node, nodeChecked);

                    }

                }

            }

            catch (Exception ex)

            {

                throw new ApplicationException(ex.Message);

            }

 

        }

      

        /// <summary>

        /// 改变父节点的选中状态

        /// </summary>

        /// <param ></param>

        /// <param ></param>

        private void checkParentNode(TreeNode curNode, bool nodeChecked)

        {

            try

            {

                bool blChecked = true;

 

                if (curNode.Parent != null)

                {

                    foreach (TreeNode node in curNode.Parent.Nodes)   //curNode.Parent.Nodes表示curNode.Parent子节点的集合

                    {

                        if (node.Checked == false)

                        {

                            blChecked = false;

                            break;

                        }

                    }

 

                    if (blChecked)

                    {

                        curNode.Parent.Checked = true;

                        curNode.Parent.ForeColor = Color.Black;

                        checkParentNode(curNode.Parent, true);

                    }

                    else

                    {

                        curNode.Parent.Checked = false;

                        curNode.Parent.ForeColor = Color.Blue;

                        checkParentNode(curNode.Parent, false);

                    }

 

                    foreach (TreeNode node in curNode.Parent.Nodes)   //curNode.Parent.Nodes表示curNode.Parent子节点的集合

                    {

                        if (node.Checked == true)

                        {

                            curNode.Parent.Checked = true;

                            break;

                        }

                    }

 

                    foreach (TreeNode node in TreRoleMenu.Nodes[0].Nodes)

                    {

                        if (node.Checked == true)

                        {

                            TreRoleMenu.Nodes[0].Checked = true;

                        }

                    }

                }

 

                this.checkParentNode(curNode);

            }

            catch (Exception ex)

            {

                throw new ApplicationException(ex.Message);

            }

 

        }

 

        /// <summary>

        /// ///判断所有子节点全部没选中,父节点颜色变为黑色

        /// </summary>

        /// <param ></param>

        private void checkParentNode(TreeNode curNode)

        {

            try

            {

                int tag = 0;

                if (curNode.Parent != null)

                {

                    if (curNode.Parent != this.TreRoleMenu.Nodes[0])

                    {

                        foreach (TreeNode node in curNode.Parent.Nodes)   //curNode.Parent.Nodes表示curNode.Parent子节点的集合

                        {

                            if (node.Checked == false)

                            {

                                tag++;

                                if (tag == curNode.Parent.Nodes.Count)

                                {

                                    curNode.Parent.ForeColor = Color.Black;

                                }

                            }

 

                        }

                    }

 

                    //tag = 0;

                    //foreach (TreeNode node in TreRoleMenu.Nodes[0].Nodes)

                    //{

                    //    if (node.Checked == false)

                    //    {

                    //        tag++;

                    //        if (tag == TreRoleMenu.Nodes[0].Nodes.Count)

                    //        {

                    //            TreRoleMenu.Nodes[0].ForeColor = Color.Black;

                    //        }

                    //    }

 

                    //}

                }

            }

            catch (Exception ex)

            {

                throw new ApplicationException(ex.Message);

            }

 

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值