This is mainly about learning from the TreeView control
The method used:
string [email protected] "e:\ lyrics";
Gets the name of all folders under the specified folder, which is a path, such as: E:\ lyrics \1111111
Directory.getdirectories (path);
Gets the file name of the file under the specified path, without the path and suffix name, such as: Welcome to New York
Path.getfilenamewithoutextension (Filepath[i])
This is the collection class for the TreeNode node
TreeNodeCollection
The TreeNode node can be new and can be set to text, with the Tag property
New== Newfilepath[i];
The TreeView control has selectednode to get the selected node, with the event Tv_afterselect, and the selected node is triggered after the change has occurred
string PATH = TV. SelectedNode.Tag.ToString ();
Code:
//method must pass in the absolute path, and the nodes property of the TreeView's namePrivate voidGetnodevalue (stringpath, TreeNodeCollection TC) { //load the name of the file under the selected folder string[] FilePath =directory.getdirectories (path); //get the name of the file stringfilename =string. Empty; //get the name of the folder for(inti =0; i < filepath.length; i++) {filename=path.getfilenamewithoutextension (Filepath[i]); TreeNode TN=NewTreeNode (); Tn. Text=filename; //to save the path of each node under the TreeView nodeTC. Add (TN); //here encountered recursion, encountered the folder, first into the folder to traverse, the large TR, replaced by the small TRGetnodevalue (filepath[i],tn. Nodes); } //because the directory name cannot be clicked, get the file under the directory//get the name of the file under the folder, string[] Newfilepath =directory.getfiles (path); for(inti =0; i < newfilepath.length; i++) {filename=path.getfilenamewithoutextension (Newfilepath[i]); TreeNode TN=NewTreeNode (); Tn. Text=filename; //to save the path of each node under the TreeView nodeTn. Tag =Newfilepath[i]; Tc. Add (TN); } } //then the Click event, after which the value under the path is read and placed in the TextBox Private voidTv_afterselect (Objectsender, TreeViewEventArgs e) { Try { stringPath =TV. SelectedNode.Tag.ToString (); Txtbox.text=file.readalltext (path, Encoding.UTF8); } Catch { } }
Extended:
1. TreeView controls and common members of TreeNode
AfterSelect//Click event
Treeview.hideselection = False; Allows the selected node to remain highlighted
Node = Treeview.selectednode; The current tree node that is selected
String I = TreeView1.SelectedNode.Index.ToString ()
The index value of the current node, starting at 0, limited to the half-sibling node collection
TreeView1.SelectedNode.Text = "String"; Sets the text of the current node
string string = TreeView1.SelectedNode.Text; Gets the text of the current node
TreeView1.SelectedNode.FullPath.ToString ();
The full path of the current node, starting from the root node to the current node, with a string of "\" concatenated by the node's Text property, and no "\" after the last node
TreeView1.SelectedNode.Tag;
Additional information about the current node, which can be of various types or objects
TreeView1.SelectedNode.Name: The name of the tree node and the key (key) for that node (TreeNodes collection) in TreeNodeCollection
TreeView1.SelectedNode.Parent; The parent tree node of the current tree node.
TreeView1.SelectedNode.Level; The depth of the tree view (zero-based)
2. In the TreeView control, member nodes is actually a collection of TreeNode, so you can also use TreeNodeCollection members:
TreeNodeCollection class, TreeNodes collection class
TREEVIEW1.NODES.ADD (string);
Adds a node to the end of the tree collection as a string for display text
TREEVIEW1.NODES.ADD (TreeNode);
Adds an existing tree node to the end of the tree node collection.
TREEVIEW1.NODES.ADD (String): Creates a new tree node with the specified key and text and adds it to the collection.
TreeView1.Nodes.Find (String Key,bool Searchallchildren): Finds the tree node with the specified key, optionally searching for child nodes, and returning a list[].
Using WinForm to recursively implement a resource manager