I need to populate treeview with folders name i got from few paths. Example, I have this few paths : C:\admin\tester1\project\item1\abc, C:\admin\t开发者_JAVA技巧ester1\project\item2\abc, C:\admin\tester1\project\item1\def, C:\admin\tester1\project3\item2\ghi
Can you all provide me some idea or examples on how to put the paths into treeview?
You need to add the root node then add each level as a child of the root node or it's parent.
' Add Node objects.
Dim nodX As Node ' Declare Node variable.
' First node with 'Root' as text.
Set nodX = TreeView1.Nodes.Add(, , "r", "Root")
' This next node is a child of Node 1 ("r").
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child1", "Child")
' This next node is a child of Node 2 ("child").
Set nodX = TreeView1.Nodes.Add("child1", tvwChild, "child2", "Child 2")
This code will ad a root node called "Root" then a child node called "Child" and then a child to that child node called "Child 2". This is the best drawing I can fake in here :-)
Root
|-Child
| |-Child 2
Add a TreeView
control on a form and try this:
Option Explicit
Private Sub Form_Load()
pvAddPath TreeView1, "C:\admin\tester1\project\item1\abc"
pvAddPath TreeView1, "C:\admin\tester1\project\item2\abc"
pvAddPath TreeView1, "C:\admin\tester1\project\item1\def"
pvAddPath TreeView1, "C:\admin\tester1\project3\item2\ghi"
End Sub
Private Sub pvAddPath(oCtl As TreeView, ByVal sPath As String)
Dim lNext As Long
Dim lStart As Long
If oCtl.Nodes.Count = 0 Then
oCtl.Indentation = 0
End If
Do While lStart < Len(sPath)
lNext = InStr(lStart + 1, sPath, "\")
If lNext = 0 Then
lNext = Len(sPath) + 1
End If
On Error Resume Next
If lStart = 0 Then
oCtl.Nodes.Add(, , Left$(sPath, lNext), Left$(sPath, lNext)).Expanded = True
Else
oCtl.Nodes.Add(Left$(sPath, lStart), tvwChild, Left$(sPath, lNext), Mid$(sPath, lStart + 1, lNext - lStart - 1)).Expanded = True
End If
On Error GoTo 0
lStart = lNext
Loop
End Sub
Check out MSDN topic: Using the TreeView Control
精彩评论