当前位置:  开发笔记 > 小程序 > 正文

winform树形菜单无限级分类实例

本文介绍了“winform树形菜单无限级分类实例”,需要的朋友可以参考一下

以下实现的是一个树形菜单,每一级对应一类窗口,点击叶子节点时弹出新的窗口;

用递归和委托实现的。

第一部分功能-创建树形菜单

复制代码 代码如下:

///
         /// 创建树形菜单
         ///

         public void AddTree(int ParentID, TreeNode pNode)
         {
             // 数据库名字字段
             string strName = "Name";
             // 数据库ID字段
             string strID = "ID";
             // 数据库父级ID字段
             string strParentID = "ParentID";
             DataTable dt = typeManager.GetAllList();
             DataView dvTree = new DataView(dt);
             dvTree.RowFilter = strParentID + " = " + ParentID;
             foreach (DataRowView Row in dvTree)
             {
                 TreeNode Node = new TreeNode();
                 if (pNode == null)
                 {
                     Node.Text = Row[strName].ToString();
                     Node.Name = Row[strName].ToString();
                     Node.Tag = Row[strID].ToString();
                     Node.ImageIndex = 1;
                     this.treeView1.Nodes.Add(Node);
                     AddTree(Int32.Parse(Row[strID].ToString()), Node); //再次递归
                 }
                 else
                 { 
                     Node.Text = Row[strName].ToString();
                     Node.Name = Row[strName].ToString();
                     Node.Tag = Row[strID].ToString();
                     Node.ImageIndex = 1;
                     pNode.Nodes.Add(Node);
                     AddTree(Int32.Parse(Row[strID].ToString()), Node); //再次递归
                 }
             }
         }

         ///
         /// 主窗体加载
         ///

         ///
         ///
         private void frmTree_Load(object sender, EventArgs e)
         {
             // 根节点ID值
             int i = 0;
             this.treeView1.Nodes.Clear();
             AddTree(i, (TreeNode)null);
             treeView1.HideSelection = true;
             treeView1.ShowLines = true;
         }

第二部分功能-文件夹图标变换:

复制代码 代码如下:

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
         {
             ImageChange(e);
         }

         private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
         {
             ImageChange(e);
         }
         ///
         /// 变换文件夹图标
         ///

         ///
         public void ImageChange(TreeNodeMouseClickEventArgs e)
         {
             if (null == e.Node.FirstNode)
             {
                 e.Node.ImageIndex = 0;
                 e.Node.SelectedImageIndex = 0;
             }
             else
             {
                 if (e.Node.IsExpanded)
                 {
                     e.Node.ImageIndex = 0;
                     e.Node.SelectedImageIndex = 0;
                 }
                 else
                 {
                     e.Node.ImageIndex = 1;
                     e.Node.SelectedImageIndex = 1;
                 }
             }
         }

第三部分功能-委托打开新窗口:

复制代码 代码如下:

delegate void Mydelegate(string name, string id);
         Mydelegate md = null;
         public frmTree()
         {
             InitializeComponent();
         }
         ///
         /// 打开新窗口
         ///

         ///
         ///
         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
         {
             // 设置显示图标的变换
             if(null== e.Node.FirstNode)
             {
                 e.Node.ImageIndex = 1;
                 e.Node.SelectedImageIndex = 0;
             }
             // 打开新的窗口,每一级对应一类窗口
             if (e.Node != null && null == e.Node.FirstNode)
             {
                 string tag =e.Node.Tag.ToString();
                 string name = e.Node.Text.ToString();
                 switch(e.Node.Level)
                 {
                     case 0:
                         this.md= new Mydelegate(OpenForm1);
                         break;
                     case 1:
                         this.md = new Mydelegate(OpenForm2);
                         break;
                     case 2:
                         this.md = new Mydelegate(OpenForm3);
                         break;
                 }
                 md(name, tag);
             }
         }
         ///
         /// 打开新窗口
         ///

         /// 传递参数
         /// 传递参数
         public static void OpenForm1(string name,string id)
         {
             Form newForm = new Form1();
             newForm.ShowDialog();
         }
         public static void OpenForm2(string name, string id)
         {
             Form newForm = new Form2();
             newForm.ShowDialog();
         }
         public static void OpenForm3(string name, string id)
         {
             Form newForm = new Form3();
             newForm.ShowDialog();
         }

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有