Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Treeview node folder images

Treeview node folder images

Scheduled Pinned Locked Moved C#
questionhelptutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rfresh
    wrote on last edited by
    #1

    I'm trying to add folder images to my treeview component. I have looked at some examples but cannot quite understand the process. I think I have to start with setting my images resources in an ImageList object right? I'm not sure how to do that. Then as I build my treeview, how do I detect a folder from a file and if a folder, set an image to it to display a folder? Thanks for any help.

    B R S 3 Replies Last reply
    0
    • R rfresh

      I'm trying to add folder images to my treeview component. I have looked at some examples but cannot quite understand the process. I think I have to start with setting my images resources in an ImageList object right? I'm not sure how to do that. Then as I build my treeview, how do I detect a folder from a file and if a folder, set an image to it to display a folder? Thanks for any help.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      I suggest you move this question to the QA forum, and tag it so we know exactly what you are working with: C#, WinForm, or whatever. Also, please indicate if you are using the standard Microsoft TreeView, that's part of Visual Studio, or, if not, what control You are using. There are many resources already here on CodeProject demonstrate enumerating a drive/directory structure recursively, and setting TreeNode images: did you search ? yours, Bill

      ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

      R 2 Replies Last reply
      0
      • B BillWoodruff

        I suggest you move this question to the QA forum, and tag it so we know exactly what you are working with: C#, WinForm, or whatever. Also, please indicate if you are using the standard Microsoft TreeView, that's part of Visual Studio, or, if not, what control You are using. There are many resources already here on CodeProject demonstrate enumerating a drive/directory structure recursively, and setting TreeNode images: did you search ? yours, Bill

        ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

        R Offline
        R Offline
        rfresh
        wrote on last edited by
        #3

        (1) I searched outside of this forum but I will search inside and see what I can find. (2) I don't see a way to move a topic? I don't see a Move button or link. I'd be glad to move this to QA if I can see how to do that. Thanks...

        B 1 Reply Last reply
        0
        • R rfresh

          (1) I searched outside of this forum but I will search inside and see what I can find. (2) I don't see a way to move a topic? I don't see a Move button or link. I'd be glad to move this to QA if I can see how to do that. Thanks...

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Hi, The simplest way is to just copy your OP, paste it in a text-editor so you don't accidentally lose it, delete the question here, and then post a new question on QA, paste in the previous question, and then select the appropriate tags. If you do search CP, I think you'll find existing articles, and answers to other questions, will give you what you need quickly. If you get "stuck," I, and I am sure others, will respond on QA. yours, Bill

          ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

          1 Reply Last reply
          0
          • B BillWoodruff

            I suggest you move this question to the QA forum, and tag it so we know exactly what you are working with: C#, WinForm, or whatever. Also, please indicate if you are using the standard Microsoft TreeView, that's part of Visual Studio, or, if not, what control You are using. There are many resources already here on CodeProject demonstrate enumerating a drive/directory structure recursively, and setting TreeNode images: did you search ? yours, Bill

            ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

            R Offline
            R Offline
            rfresh
            wrote on last edited by
            #5

            The Delete link is disabled so I can't delete it to move it. I did a search and 'figured out' that I could populate the imagelist property in my treeview with my needed images. I have a brown folder for directories and a green folder for files but in my code below, the folders don't get the brown folder icon and the one file does not get the green folder! I thought after I understand finally that the index into the images could be used to set the folder icon. Below in directory loop I'm setting 0 index which has the brown folder and the loop below that one is for files and I set index 1 which is the second green folder image.

            foreach (var directory in directoryInfo.GetDirectories())
            {
            var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
            currentNode.Nodes.Add(childDirectoryNode);
            currentNode.ImageIndex = 0;
            stack.Push(childDirectoryNode);
            }
            foreach (var file in directoryInfo.GetFiles())
            {
            currentNode.Nodes.Add(new TreeNode(file.Name));
            currentNode.ImageIndex = 1;
            }

            B 1 Reply Last reply
            0
            • R rfresh

              The Delete link is disabled so I can't delete it to move it. I did a search and 'figured out' that I could populate the imagelist property in my treeview with my needed images. I have a brown folder for directories and a green folder for files but in my code below, the folders don't get the brown folder icon and the one file does not get the green folder! I thought after I understand finally that the index into the images could be used to set the folder icon. Below in directory loop I'm setting 0 index which has the brown folder and the loop below that one is for files and I set index 1 which is the second green folder image.

              foreach (var directory in directoryInfo.GetDirectories())
              {
              var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
              currentNode.Nodes.Add(childDirectoryNode);
              currentNode.ImageIndex = 0;
              stack.Push(childDirectoryNode);
              }
              foreach (var file in directoryInfo.GetFiles())
              {
              currentNode.Nodes.Add(new TreeNode(file.Name));
              currentNode.ImageIndex = 1;
              }

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              Re: can't delete message: I guess that once responses have been made to a post it can't be deleted on this type of forum. That's something I've never thought about before, and I apologize if I misled you. Looking at your code, I can't quite follow what you're doing: your pushing Directories on a Stack, in the first loop, but in your second loop, traversing the files, I can't see where the Stack is being used. Neither loop shows where 'currentNode is being set. And, 'childDirectoryNode is, of course, contained in the scope of the loop that traverses the Directories. My guess is: the two loops you show are separate pieces of code, and that they are connected in some way not shown, perhaps in a recursive outer method ? Are you using WinForms, and the MS standard TreeView ? Do keep in mind that for the WinForms TreeView you can set the default Node image at design-time by either the 'ImageIndex or 'ImageKey property of the TreeView (assuming you've set the 'ImageList property of the TreeView to a valid ImageList). Those two properties are "mutually exclusive:" you set one, and it "disables" the other. I'd suggest you start by finding an example that uses just recursion, and doesn't use a Stack (although using a Stack to reduce the "overhead" of recursion is a very good thing). good luck, Bill

              ~ “This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal

              1 Reply Last reply
              0
              • R rfresh

                I'm trying to add folder images to my treeview component. I have looked at some examples but cannot quite understand the process. I think I have to start with setting my images resources in an ImageList object right? I'm not sure how to do that. Then as I build my treeview, how do I detect a folder from a file and if a folder, set an image to it to display a folder? Thanks for any help.

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                See this[^] article. /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • R rfresh

                  I'm trying to add folder images to my treeview component. I have looked at some examples but cannot quite understand the process. I think I have to start with setting my images resources in an ImageList object right? I'm not sure how to do that. Then as I build my treeview, how do I detect a folder from a file and if a folder, set an image to it to display a folder? Thanks for any help.

                  S Offline
                  S Offline
                  SaqibRasheed
                  wrote on last edited by
                  #8

                  You should create an ImageList and then assign images from the list to the nodes like node.ImageIndex = 0; this will assign the image at index =0 to the node. Better assign the images to the nodes while creating that node; either that node is the folder node or a list node.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups