Results 1 to 4 of 4

Thread: Add Icons to TreeView

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Add Icons to TreeView

    I recently just added directories and files to the treeView. How would I go about adding an icon for the folder nodes, and then a different icon for the file nodes? Thank you very much.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Icons to TreeView

    Images are displayed against nodes in a TreeView the same way they are displayed against items in a ListView. You create an ImageList and assign it to the ImageList property of the TreeView. You add Images to the Images collection of the ImageList and then set the ImageIndex or ImageKey of each node to specify which Image gets displayed against it.

    You'll need to add one Image for the folders (or one open and one closed if you want get fancy) and one for each file type. How those Images are created is completely up to you. You can add them at design time or run time. If you want to get the system icon for a file then you can call Icon.ExtractAssociatedIcon to get an Icon object and then call ToBitmap on that to get a Bitmap object that can be added to an ImageList.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: Add Icons to TreeView

    Thank you. Here is my code so far:

    Code:
    private static void ListDirectory(TreeView treeView, string path)
            {
                treeView.Nodes.Clear();
    
                ImageList myImageList = new ImageList();
                myImageList.Images.Add(Properties.Resources.foldericon); //icon 0
                myImageList.Images.Add(Properties.Resources.html_icon); //icon 1
                treeView.ImageList = myImageList;
    
                var stack = new Stack<TreeNode>();
                var rootDirectory = new DirectoryInfo(path);
                var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
                stack.Push(node);
                treeView.Nodes.Add(node);
                while (stack.Count > 0)
                {
                    var currentNode = stack.Pop();
                    var directoryInfo = (DirectoryInfo)currentNode.Tag;
                    foreach (var directory in directoryInfo.GetDirectories())
                    {
                        var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
                        currentNode.Nodes.Add(childDirectoryNode);
                        treeView.Nodes.Add(directory.Name, directory.Name, 0);
                        stack.Push(childDirectoryNode);
                    }
                    foreach (var file in directoryInfo.GetFiles("*.htm"))
                    {
                        currentNode.Nodes.Add(new TreeNode(file.Name));
                        treeView.Nodes.Add(file.Name, file.Name, 1);
                    }
                }
    
            }
    But there is an error in the TreeView:
    Name:  treeview.png
Views: 509
Size:  1.8 KB

    'folder' is the root directory, with a subdirectory, 'sub', and then a file in the sub, 'index.html'. Those get added, but they get added again right below it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Icons to TreeView

    A tree is an inherently recursive structure so, unless you have a compelling reason to do otherwise, it makes sense to use recursion to populate it, e.g.
    vb.net Code:
    1. Private Function GetFileSystemNodes(folderPath As String) As TreeNode()
    2.     Dim nodes As New List(Of TreeNode)
    3.  
    4.     Try
    5.         For Each subfolderPath In Directory.GetDirectories(folderPath)
    6.             nodes.Add(New TreeNode(Path.GetFileName(subfolderPath), 0, 0, GetFileSystemNodes(subfolderPath)) With {.Tag = subfolderPath})
    7.         Next
    8.  
    9.         For Each filePath In Directory.GetFiles(folderPath, "*.htm")
    10.             nodes.Add(New TreeNode(Path.GetFileName(filePath), 1, 1) With {.Tag = filePath})
    11.         Next
    12.     Catch ex As UnauthorizedAccessException
    13.         'Ignore inaccessible folder and move on.
    14.     End Try
    15.  
    16.     Return nodes.ToArray()
    17. End Function
    Sample usage:
    Code:
    Dim rootFolderPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    Dim rootNode As New TreeNode(rootFolderPath, 0, 0, GetFileSystemNodes(rootFolderPath)) With {.Tag = rootFolderPath}
    
    TreeView1.Nodes.Add(rootNode)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width