我需要使用从少数路径获得的文件夹名称填充treeview。例如,我有这几条路径: C:\ ADMIN \ tester1 \项目\物品1 \ ABC, C:\ ADMIN \ tester1 \项目\ ITEM2 \ ABC, C:\ ADMIN \ tester1 \项目\物品1 \ DEF, C:\管理员\ tester1 \项目3 \ ITEM2 \ GHI
您能否就如何将路径放入树视图向我提供一些想法或示例?
答案 0 :(得分:3)
您需要添加根节点,然后将每个级别添加为根节点的子节点或其父节点。
' 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")
此代码将添加一个名为“Root”的根节点,然后是一个名为“Child”的子节点,然后是一个名为“Child 2”的子节点的子节点。这是我能在这里假装的最佳图画: - )
根
| - 儿童
| | -Child 2
答案 1 :(得分:0)
在表单上添加TreeView
控件并尝试:
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
答案 2 :(得分:0)
查看MSDN主题:Using the TreeView Control