【问题标题】:vb6 - populate treeviewvb6 - 填充树视图
【发布时间】:2011-03-02 03:17:43
【问题描述】:

我需要使用从几个路径中获得的文件夹名称填充树视图。例如,我有这几条路径: C:\admin\tester1\project\item1\abc, C:\admin\tester1\project\item2\abc, C:\admin\tester1\project\item1\def, C:\admin\tester1\project3\item2\ghi

你们能给我一些关于如何将路径放入树视图的想法或示例吗?

【问题讨论】:

    标签: vb6 treeview


    【解决方案1】:

    您需要添加根节点,然后将每个级别添加为根节点的子节点或其父节点。

    ' 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”。这是我可以在这里伪造的最好的图画:-)


    |-儿童
    | |-孩子2

    【讨论】:

    • 谢谢。但是,在将每个路径分成几个文件夹后,我不知道它属于哪个父级。另外,如何检查根是否已经存在
    【解决方案2】:

    在表单上添加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
    

    【讨论】:

      【解决方案3】:

      查看 MSDN 主题:Using the TreeView Control

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多