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

winform树形菜单无限级分类实例,第1张

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

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

用递归和委托实现的。

第一部分功能-创建树形菜单
复制代码 代码如下:
/// <summary>
         /// 创建树形菜单
         /// </summary>
         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); //再次递归
                 }
             }
         }

         /// <summary>
         /// 主窗体加载
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         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);
         }
         /// <summary>
         /// 变换文件夹图标
         /// </summary>
         /// <param name="e"></param>
         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();
         }
         /// <summary>
         /// 打开新窗口
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         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);
             }
         }
         /// <summary>
         /// 打开新窗口
         /// </summary>
         /// <param name="name">传递参数</param>
         /// <param name="ID">传递参数</param>
         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();
         }

总结

以上是内存溢出为你收集整理的winform树形菜单无限级分类实例全部内容,希望文章能够帮你解决winform树形菜单无限级分类实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1263905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存