I recently watched the. NET video tutorial. In chapter 3, I talked about the use of the Treeview and listview controls. I encountered some problems and solved them. The record is as follows.
Code:
- Using system;
- Using system. Collections. Generic;
- Using system. componentmodel;
- Using system. Data;
- Using system. drawing;
- Using system. text;
- Using system. Windows. forms;
- Using system. IO;
- Namespace filemanager
- {
- Public partial class winform: Form
- {
- Public winform ()
- {
- Initializecomponent ();
- }
- Private void winform_load (Object sender, eventargs E)
- {
- String paths = @ "E:/video ";
- Trvcontent. nodes. Add (paths, "video ");
- Sortfile (paths, trvcontent. nodes [0]);
- }
- Private void sortfile (string paths, treenode Tn)
- {
- String [] dir = directory. getdirectories (paths );
- Foreach (string s in Dir)
- {
- Treenode Tn = tn. nodes. Add (S, path. getfilename (s ));
- Sortfile (S, TN );
- }
- }
- Private void trvcontent_nodemouseclick (Object sender, treenodemouseclickeventargs E)
- {
- Lsvfile. Items. Clear ();
- String [] dir = directory. getdirectories (E. node. Name );
- Foreach (string s in Dir)
- {
- Lsvfile. Items. Add ("", path. getfilename (s), 1 );
- }
- String [] file = directory. getfiles (E. node. Name );
- Foreach (string SS in file)
- {
- Lsvfile. Items. Add ("", path. getfilename (SS), 0 );
- }
- }
- }
- }
The result is a simple simulation of the resource manager.
Question 1: Add a node
Initially used
Trvcontent. nodes. Add ("video ");
Tn. nodes. Add (path. getfilename (s); add a node
After adding a node, you will find that the click node function is not easy to implement, because path. getfilename (s); only displays the file name, not the path name. The path is lost and cannot be found ......
Solution 1:
Code:
- Private void winform_load (Object sender, eventargs E)
- {
- String paths = @ "E:/video ";
- Trvcontent. nodes. Add (paths, "video"); // here, the add (string key, string text) overload is used, and the key value provides the path
- Sortfile (paths, trvcontent. nodes [0]); // recursive call
- }
Question 2: Click the node to retrieve the files and folders in the path and display them to listview.
The first is the nodemouseclick event, and then obtains the path of the clicked node ...... By video
String [] dir = directory. getdirectories (E. node. Name); however, an error occurs.
Solution 2:
The tracing found that E. node. Name is null. How can this happen. I watched the video again and found the problem ......
I wrote it like this.
Code:
- Trvcontent. nodes. Add ("video ");
- Le (paths, trvcontent. nodes [0]);
Because the first root node does not have a reserved path ...... Unable to obtain
The instructor wrote:
Code:
- String paths = @ "E:/video ";
- Trvcontent. nodes. Add (paths, "video"); // here, the add (string key, string text) overload is used, and the key value provides the path
- Sortfile (paths, trvcontent. nodes [0]); // recursive call
Code:
- String [] dir = directory. getdirectories (E. node. Name); // If add (string key, string text) is not used, an error is returned.
- // Because the key provides the path, and E. node. Name reads the key value
Monitoring finds that the key of the overload add provides the name value ......
Question 3: add the listview file icon
Code:
- Svfile. Items. Add ("", path. getfilename (s), 1); // Add (string key, string text, int imageindex) is used here.
- } // Numbers 1 and 0 represent the index value of the image stored in the imagelist space.
I have added the image to imagelist, but it is not displayed.
Solution 3:
The problem is that listview is not associated with imagelist.