程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> winform樹形菜單無限級分類實例

winform樹形菜單無限級分類實例

編輯:C#基礎知識

以下實現的是一個樹形菜單,每一級對應一類窗口,點擊葉子節點時彈出新的窗口;

用遞歸和委托實現的。

第一部分功能-創建樹形菜單
代碼如下:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved