Explorer Like Folder View Help

coleman01

Member
Joined
Apr 8, 2005
Messages
11
Programming Experience
Beginner
Hi,

I wondered if anyone could help me (again). In a database, I have a list of users and their portfolio paths. I have got them to load into a list box. I need it so when a user clicks on a user ID from the list box, it shows the content of the folder in an explorer style window (using icons).

Being good at VB6, I can do that quite easily and the standard TreeView in .net is a bit dull and I have no idea how to do what I need to do.

Could anyone help me with this problem? Again, I have had a look around, but I am not sure about this.

My sincere thanks.
 
You can use the Icon.ExtractAssociatedIcon method to get an Icon to represent any file. It will contain the same icon that Windows Explorer would use for the same file.
 
To have icons associated with the treeview control items you need an ImageList object.
-Drag an ImageList control from the toolbox onto your form.
-Select your treeview control, look in the properties for "ImageList" and set that property to the name of your image list control (by default it's ImageList1).
-You can add images to the ImageList control manually. Right click it and use the "Add Images". Or else you can add images to your ImageList control programatically using ImageList1.Images.Add method.
-Now the ImageList is ready to use.

To add nodes to the treeview's root you should use something like that:
Dim node1 as TreeNode
node1 = Treeview1.Nodes.Add("This is a root node")
node1.ImageIndex=10
node1.SelectedImageIndex=27
What it does: Creates a node and returns it to the node1 variable. Sets the image associated with the node for the unselected state (in our case is the 10-th image in the ImageList control). Sets the image for the node in selected state for the 27-th image in the ImageList control.

(Notice: To keep explanations simple and clear I intentionally ignored a detail. The images in the ImageList have zero-based index, so actually you will get the 11-th, and the 28-th images with those numbers.)

If you want to add more root nodes, repeat this code multiple times.

To add child nodes to the root nodes already created, you need the TreeNode object (in our case node1) and set it up in a similar manner:
Dim node2 as TreeNode
node2 = node1.Nodes.Add("This is a child node")
node2.ImageIndex=14
node2.SelectedImageIndex=53

The difference between the ImageIndex and the SelectedImageIndex it's obvious, you can notice in the left pane of the Windows Explorer how the closed folder image turns into an open folder image when the folder is selected.
 
Icon.ExtractAssociatedIcon
srelu told me this in another thread also today, I was unaware of it. Nice method.

Something else that could be useful is that ImageList now also supports string key indexing (also used by Treenode and ListViewItem). This could be useful for example to store icons based on file extension.
 
Back
Top