PDA

View Full Version : Solved: Treeview & Imagelist relationship



lhardee
04-08-2008, 08:40 PM
Hello to all

:think:

I've been searching and searching and can't find an answer. I am trying to find a way to create a Treeview on a form that recogizes file type and associates an image accordingly. Currently my Treeview points to a folder on my C drive which is full of either .xls or .txt files and a few other folders.

I have 4 images - "closed folder", "opened folder", "image for .txt file" and "image for .xls file".

Anyone know how to accomplish this ?

Thank you in advance.

rory
04-09-2008, 05:46 AM
When you populate the Treeview, one of the arguments for Nodes.Add is the image to assign to a particular node. Or is that not what you are asking?

lhardee
04-09-2008, 08:46 AM
Hi Rory,

And thank you for the reply.

I'm not sure how to associate the imagelist to recognize different file types. I've attached an example of my treeview and imagelist if it helps you understand what I am trying to do.

You will have to change the address below:


Private Sub UserForm_activate()

Call TreeViewPopulate("C:\RES\HealthReport 10-03\")
Application.StatusBar = ""

End Sub

rory
04-09-2008, 09:02 AM
You have to determine the image yourself. In the part where you add nodes for each file, you need to incorporate a check on the extension, and then choose the relevant image from the list. There is no automatic link between the imagelist and the file type.

lhardee
04-26-2008, 08:39 AM
Hello all,

With rory's help, I feel I'm a bit closer but still not there yet. I wish I could say that I knew what I was doing.:( Been messing with VBA for a little while, and only know enough to be dangerous.

I am trying to get my treeview to relate a certain image with *.xls files and *.txt files. but am having no luck..

The treeview works until I call LoopFolders (to add nodes to each filetype)...then I get a compile error.



Private Sub UserForm_Initialize()

With TreeView1

.Appearance = cc3D
.Indentation = 12

'Call LoopFolders
End With

End Sub



lhardee

lhardee
04-27-2008, 09:52 AM
:help : pray2:

Can anyone shed some light on this ?

lhardee

lhardee
04-27-2008, 08:56 PM
Okay...after going over Rory's suggestion a few more times and researching on the web until I can't see straight :bug: , I think I understand a little better.

I think I made more progress by changing some things. I feel as though I am very close now in that when I run the treeview, all that is missing now is the icons that I selected for each filetype. I think it has something to do with the following code:


If fil.Name Like "*.txt" Then

TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

End If


I think I should be referencing the particular icon that I want shown for .txt files.

Any suggestions would be greatly appreciated !!

If I luckily stumble onto the answer, I will post my results.

lhardee

lhardee
04-28-2008, 07:47 AM
Hello All,

Back at it again ! I noticed many mistakes and tried to fix what I could. All code used has been pieced together either found on this board or others. As I mentioned, my VBA skills are....well...I have no skills. :think:

I came across another treeview example which I tried to emulate, with no success.

http://puremis.net/excel/code/080.shtml

The changes I made were to imbed pictures on my worksheet and get rid of the imagelist control.

I'm still at a dead end though. Anyone out there care to take a stab at this ?

I think my problem is with the code below:
I'm not sure, but I need to reference which image to display, but nothing I've tried is working.



Private Sub GetFiles(fld As Folder, Optional metro As Folder = Null)

Dim son As Folder
Dim fil As file
Dim nod As Node

On Error Resume Next

If metro Is Nothing Then

Set nod = TreeView1.Nodes.Add(, , fld.Name, fld.Name)

nod.Expanded = True

Else

TreeView1.Nodes.Add metro.Name, tvwChild, fld.Name, fld.Name

End If


For Each son In fld.SubFolders

Call GetFiles(son, fld)

'TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

Next

For Each fil In fld.Files

If fil.Name Like "*.txt" Then

TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

End If

If fil.Name Like "*.xls" Then

TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

End If
Next

End Sub



lhardee

rory
04-28-2008, 09:43 AM
It's easier to use an imagelist, IMO. If your imagelist has two pictures in it, first for text files, second for xls, then your routine becomes:
Private Sub GetFiles(fld As Folder, Optional metro As Folder = Null)

Dim son As Folder
Dim fil As File
Dim nod As Node

On Error Resume Next

If metro Is Nothing Then

Set nod = tvwMain.Nodes.Add(, , fld.Name, fld.Name)

nod.Expanded = True

Else

tvwMain.Nodes.Add metro.Name, tvwChild, fld.Name, fld.Name

End If


For Each son In fld.SubFolders

Call GetFiles(son, fld)

'TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

Next

For Each fil In fld.Files

If fil.Name Like "*.txt" Then

tvwMain.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name, 1

End If

If fil.Name Like "*.xls" Then

tvwMain.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name, 2

End If
Next

End Sub


and you need to set the ImageList as the ImageList for the Treeview.

lhardee
04-28-2008, 10:17 AM
rory,

Without your help I would be stuck forever. Thanks so much for your guidance. I've made the changes you suggested.

I must be doing something wrong. At first, I received a compile error for tvwMain. AFter deleting Option Explicit above, the form worked....but the form displays an empty treeview.

Attached are the changes I made.


And Thank you for getting me this far :thumb

lhardee

rory
04-28-2008, 12:08 PM
Sorry, I was testing in a form I had and had to change the name to match my treeview. Your code should be:
Private Sub GetFiles(fld As Folder, Optional metro As Folder = Null)

Dim son As Folder
Dim fil As File
Dim nod As Node

On Error Resume Next

If metro Is Nothing Then

Set nod = TreeView1.Nodes.Add(, , fld.Name, fld.Name)

nod.Expanded = True

Else

TreeView1.Nodes.Add metro.Name, tvwChild, fld.Name, fld.Name

End If


For Each son In fld.SubFolders

Call GetFiles(son, fld)

'TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name

Next

For Each fil In fld.Files

If fil.Name Like "*.txt" Then

TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name, 1

End If

If fil.Name Like "*.xls" Then

TreeView1.Nodes.Add fld.Name, tvwChild, fil.Path, fil.Name, 2

End If
Next

End Sub

As a general note, you should never have to remove Option Explicit to get code to run. If you do, then you haven't declared variables or your code is wrong! :)

lhardee
04-28-2008, 01:35 PM
Rory,

If I had the $$$, I'd fly to Ireland and buy you a keg of beer. :beerchug:

The only thing I had to change was to call SettingImageList during form_activate instead of initialization.

Here are the results:

Only thing I have to figure out now is how to reduce the picture size.

Thanks again rory....outstanding !

lhardee