Treeview Control Problems

Source: Internet
Author: User

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:
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. componentmodel;
  4. Using system. Data;
  5. Using system. drawing;
  6. Using system. text;
  7. Using system. Windows. forms;
  8. Using system. IO;
  9. Namespace filemanager
  10. {
  11. Public partial class winform: Form
  12. {
  13. Public winform ()
  14. {
  15. Initializecomponent ();
  16. }
  17. Private void winform_load (Object sender, eventargs E)
  18. {
  19. String paths = @ "E:/video ";
  20. Trvcontent. nodes. Add (paths, "video ");
  21. Sortfile (paths, trvcontent. nodes [0]);
  22. }
  23. Private void sortfile (string paths, treenode Tn)
  24. {
  25. String [] dir = directory. getdirectories (paths );
  26. Foreach (string s in Dir)
  27. {
  28. Treenode Tn = tn. nodes. Add (S, path. getfilename (s ));
  29. Sortfile (S, TN );
  30. }
  31. }
  32. Private void trvcontent_nodemouseclick (Object sender, treenodemouseclickeventargs E)
  33. {
  34. Lsvfile. Items. Clear ();
  35. String [] dir = directory. getdirectories (E. node. Name );
  36. Foreach (string s in Dir)
  37. {
  38. Lsvfile. Items. Add ("", path. getfilename (s), 1 );
  39. }
  40. String [] file = directory. getfiles (E. node. Name );
  41. Foreach (string SS in file)
  42. {
  43. Lsvfile. Items. Add ("", path. getfilename (SS), 0 );
  44. }
  45. }
  46. }
  47. }

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:
  1. Private void winform_load (Object sender, eventargs E)
  2. {
  3. String paths = @ "E:/video ";
  4. Trvcontent. nodes. Add (paths, "video"); // here, the add (string key, string text) overload is used, and the key value provides the path
  5. Sortfile (paths, trvcontent. nodes [0]); // recursive call
  6. }

 

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:
  1. Trvcontent. nodes. Add ("video ");
  2. Le (paths, trvcontent. nodes [0]);

Because the first root node does not have a reserved path ...... Unable to obtain

The instructor wrote:

Code:
  1. String paths = @ "E:/video ";
  2. Trvcontent. nodes. Add (paths, "video"); // here, the add (string key, string text) overload is used, and the key value provides the path
  3. Sortfile (paths, trvcontent. nodes [0]); // recursive call
Code:
  1. String [] dir = directory. getdirectories (E. node. Name); // If add (string key, string text) is not used, an error is returned.
  2. // 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:
  1. Svfile. Items. Add ("", path. getfilename (s), 1); // Add (string key, string text, int imageindex) is used here.
  2. } // 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.