关闭

TreeView的使用

488人阅读 评论(0) 收藏 举报

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace Charge
{
    public class TreeFunction
    {
        public void TreeRoot(ref TreeView myT, Form myForm)
        {

            foreach (MenuItem myMI in myForm.Menu.MenuItems)
            {
                TreeNode myTreeN = new TreeNode();
                myTreeN.Text = myMI.Text;
                TreeNod(myMI, myTreeN, false);
                myT.Nodes.Add(myTreeN);
            }
            myT.ShowRootLines = true;
            myT.SelectedNode = myT.Nodes[0];
            myT.Focus();
        }
        public TreeNode TreeNod(System.Windows.Forms.MenuItem m, TreeNode asTN, bool asbl)
        {
            TreeNode TreeN1 = new TreeNode();
            //if (asbl && m.Text!=System.Convert.ToString('-'))
            if (asbl)
            {
                TreeN1.Text = m.Text;
                asTN.Nodes.Add(TreeN1);
            }
            else
            {
                TreeN1 = asTN;
            }
            foreach (System.Windows.Forms.MenuItem subm in m.MenuItems)
            {
                TreeNod(subm, TreeN1, true);
            }
            return TreeN1;
        }
        public void TreeSetRightByUserGroup(string UserGroupbm, TreeNodeCollection myTree, wglpforever  myConM)
        {
            DataSet myDS;
            foreach (TreeNode tn in myTree)
            {
                string lsSql = "select * from drv_bksf.drv_usergroupmenu where usergroupbm='" + UserGroupbm + "' and usergroupmenu='" + tn.Text + "'";
                myDS = myConM.Data_Fill(lsSql, "drv_fei_usergroupmenu");
                if (myDS == null)
                {
                    return;
                }
                if (myDS.Tables[0].Rows.Count == 1)
                {
                    tn.Checked = true;

                }
                else
                {
                    tn.Checked = false;
                }
                myDS.Clear();
                if (tn.Nodes.Count != 0)
                    TreeSetRightByUserGroup(UserGroupbm, tn.Nodes,myConM);
            }
        }
        //选择子节点
        public void TreeSelectRight(TreeNodeCollection myTree, bool myIndex)
        {
            foreach (TreeNode tn in myTree)
            {
                tn.Checked = myIndex;
            }
        }
        //选择父节点
        public void TreeSelectParent(TreeNode Tn, bool lsCheck)
        {
            if (Tn.Parent == null) return;
            switch (lsCheck)
            {
                case true:
                    Tn.Parent.Checked = true;
                    break;
                case false:
                    Tn.Parent.Checked = false;
                    break;
                default:
                    break;
            }
        }
        public int TreeSaveRight(string UserGroupbm, TreeNodeCollection myTree, wglpforever myConM)
        {

            foreach (TreeNode tn in myTree)
            {
                if (tn.Checked == true)
                {
                    string lsSql = "insert into  drv_bksf.drv_usergroupmenu(usergroupbm,usergroupmenu) values('" + UserGroupbm + "','" + tn.Text + "')";
                    try
                    {
                        myConM.IUDBySql(lsSql);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString(), "error");
                        return 0;
                    }
                }
                if (tn.Nodes.Count != 0)
                    TreeSaveRight(UserGroupbm, tn.Nodes, myConM);
            }
            return 1;
        }
    }
}
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3949次
    • 积分:78
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条
    文章分类