I read the article on saving the contents of a treeview, and I am having trouble with it. It only saves the first folder in the treeview, and creates a ton of blank nodes. Anyone know how to do this
Hi Robcrowell,
Here is a simple example of how u can save & restore the content of a treeview.
Place a treeview (tree1) and Imagelist on the form and add few images. Make sure you set the ImageList Property of the treeview to ImageList1
Code:
Option Explicit
Dim i As Integer
Private Sub Form_Load()
Dim i As Integer
Dim Parent As String, Key As String, Text As String, Image As Integer, Expanded As Boolean
Open "C:\del.txt" For Input As #1
Do While Not EOF(1)
Input #1, Parent, Key, Text, Image, Expanded
If Key <> "Root" Then
Tree1.Nodes.Add Parent, tvwChild, Key, Text, Image
Else
Tree1.Nodes.Add , , Key, Text, Image
End If
If Expanded = True Then
Tree1.Nodes(i + 1).Expanded = True
Else
Tree1.Nodes(i + 1).Expanded = False
End If
i = i + 1
Loop
Close #1
If Tree1.Nodes.Count = 0 Then
Tree1.Nodes.Add , , "Root", "Root", 1
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Open "C:\del.txt" For Output As #1
For i = 1 To Tree1.Nodes.Count
If Tree1.Nodes(i).Text = "Root" Then
Write #1, "", Tree1.Nodes(i).Key, Tree1.Nodes(i).Text, Tree1.Nodes(i).Image, Tree1.Nodes(i).Expanded
Else
Write #1, Tree1.Nodes(i).Parent.Key, Tree1.Nodes(i).Key, Tree1.Nodes(i).Text, Tree1.Nodes(i).Image, Tree1.Nodes(i).Expanded
End If
Next i
Close #1
End Sub
Private Sub mnuNodesAdd_Click()
Dim Key As String
Key = "Key" & Int(Rnd * 10000000)
Tree1.Nodes.Add Tree1.SelectedItem.Key, tvwChild, Key, "Item " & i + 1, 2
Tree1.SelectedItem.Expanded = True
Tree1.Nodes(Key).Selected = True
Tree1.StartLabelEdit
i = i + 1
End Sub
I am Having trouble with this code. Can you help me get it to work? My code is included:
'In Module
Option Explicit
Private FSO As Scripting.FileSystemObject
Public Sub Folder2Treeview(Path As String, Dest As TreeView, Optional IsParent As Boolean = True)
Dim FSOFolder As Scripting.Folder
Dim FSOSubFolder As Scripting.Folder
Dim FSOFile As Scripting.File
Dim Key As String
' Get a directory listing of the path
Set FSOFolder = FSO.GetFolder(Path)
' Get all subfolders first
For Each FSOSubFolder In FSOFolder.SubFolders
' Add it to the treeview
If IsParent Then
Dest.Nodes.Add "Main", tvwChild, Path & FSOSubFolder.Name & "\", FSOSubFolder.Name, 1
Else
Dest.Nodes.Add Path, tvwChild, Path & FSOSubFolder.Name & "\", FSOSubFolder.Name, 1
End If
' Now get a directory listing for that path too
Folder2Treeview Path & FSOSubFolder.Name & "\", Dest, False
Next
' Get all files
For Each FSOFile In FSOFolder.Files
' Add it to the treeview
If IsParent Then
Dest.Nodes.Add "Main", tvwChild, , FSOFile.Name, 3
Else
Dest.Nodes.Add Path, tvwChild, , FSOFile.Name, 3
End If
Next
End Sub
Public Sub Initialize()
' Create FileSystemObject
Set FSO = New Scripting.FileSystemObject
End Sub
'In Form
Private Sub cmdBuild_Click()
' List the currect directory, don't forget to
' add a '\' at the end or it will mess up!
Tree1.Nodes.Add , , "Main", "Main Folder", 1
modTVFolder.Folder2Treeview "C:\Visual Basic 6\VB98\", Tree1
End Sub
Private Sub Form_Load()
' Initialize FileSystemObject
modTVFolder.Initialize
End Sub
Private Sub tvFolder_Collapse(ByVal Node As MSComctlLib.Node)
' Change from open to closed folder
If Node.Image = 2 Then Node.Image = 1
End Sub
Private Sub tvFolder_Expand(ByVal Node As MSComctlLib.Node)
' Change from closed to open folder
If Node.Image = 1 Then Node.Image = 2
End Sub
'End Form Code
Well, I realize thats a lot of code, so i attached my project as well. Your help greatly appreciated, Rob