Tree loaded and bound right-click event

 1 using System.Collections;
 2 using System.Drawing;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Windows.Forms;
 6 
 7 namespace menuTreeWITHrightMouseClick
 8 {
 9     public partial class Form1 : Form
10     {
11         string strRootFolderPath = @"D:\CODE";
12         TreeNode rootNode;
13 
14         public Form1()
15         {
16             InitializeComponent();
17             rootNode = new TreeNode(strRootFolderPath);
18             BangdingTreeView(rootNode);
19             this.tv_Folders.Nodes.Add(rootNode);
20             this.tv_Folders.CollapseAll();
21             int a = this.tv_Folders.GetNodeCount(true);
22         }
23 
24         private void BangdingTreeView(TreeNode tr)
25         {
26             foreach (string strPath in Directory.GetDirectories(tr.Text))
27             {
28                 TreeNode currentNode = new TreeNode(strPath);
29                 GetTreeNodesStatus(tv_Folders.Nodes);
30                 tr.Nodes.Add(currentNode);
31                 //更新TreeView函数
32                 SetTreeNodesStatus(tv_Folders.Nodes);
33                 if (Directory.GetDirectories(strPath).Count() > 0)
34                 {
35                     BangdingTreeView(currentNode);
36                 }
37             }
38         }
39 
40         private Hashtable NodesStatus = new Hashtable();
41         private string SelectNodeFullPath = string.Empty;
42 
43         private void GetTreeNodesStatus(TreeNodeCollection nodes)
44         {
45             foreach (TreeNode node in nodes)
46             {
47                 if (node.IsExpanded)
48                 {
49                     NodesStatus[node.FullPath] = true;
50                 }
51                 else
52                 {
53                     NodesStatus.Remove(node.FullPath);
54                 }
55                 if (node.IsSelected)
56                 {
57                     SelectNodeFullPath = node.FullPath;
58                 }
59                 GetTreeNodesStatus(node.Nodes);
60             }
61         }
62 
63         private void SetTreeNodesStatus(TreeNodeCollection nodes)
64         {
65             foreach (TreeNode node in nodes)
66             {
67                 if (NodesStatus[node.FullPath] != null)
68                 {
69                     node.Expand();
70                 }
71                 if (node.FullPath == SelectNodeFullPath)
72                 {
73                     this.tv_Folders.SelectedNode = node;
74                 }
75                 SetTreeNodesStatus(node.Nodes);
76             }
77         }
78 
79         private voidtv_Folders_MouseDown ( Object SENDER, The MouseEventArgs E)
 80          {
 81              IF (e.Button == MouseButtons.Right) // determines whether the right-click 
82              {
 83                  Point = ClickPoint new new Point (eX, eY); // Get the mouse clicks coordinates 
84                  the TreeNode the CurrentNode = tv_Folders.GetNodeAt (ClickPoint); // coordinate acquired looking at node 
85                  IF (the CurrentNode =! null ) // determination node not click location 
86                  {
 87                      CurrentNode.ContextMenuStrip = ctm_rightClickMenus;// to the current node property is bound to get right-click on the event 
88                      String name = tv_Folders.SelectedNode.Text.ToString (); // storage node text 
89                      tv_Folders.SelectedNode = CurrentNode; // the Click for the node set selected 
90                  }
 91 is              }
 92          }
 93  
94          Private  void tv_Folders_AfterSelect ( Object SENDER, TreeViewEventArgs E)
 95          {
 96  
97          }
 98      }
 99 }

 

Guess you like

Origin www.cnblogs.com/palebluestarrysky/p/11146075.html