【问题标题】:Treenode not appearing when program restarts程序重新启动时树节点不出现
【发布时间】:2014-01-21 00:26:21
【问题描述】:

下面的代码是从列表框上的双击事件运行的,该事件将选定的文件复制到树视图上的选定节点目录,然后将选定的文本添加为​​子节点。

它似乎可以正常工作,但是当程序关闭然后重新打开时,它没有显示子节点。

任何指针............

    Dim Copy2 = aMailbox & tvProgress.SelectedNode.Text & "\" & lstRequired.Text
    Dim Copy1 = rPath & "\" & lstRequired.Text

    If File.Exists(Copy2) Then
        MsgBox("File already added. Please edit from the view above", MsgBoxStyle.OkOnly, "Lynx Control Panel")
        Exit Sub

    End If

    If File.Exists(Copy1) Then
        File.Copy(Copy1, Copy2)
        tvProgress.SelectedNode.Nodes.Add(lstRequired.Text)
        tvProgress.ExpandAll()
    Else
        MsgBox("This file no longer exists in your Lynx Repository. Please select another", MsgBoxStyle.OkOnly, "Lynx Control Panel")
        Exit Sub
    End If

以下代码完全取自第一个列表框的双击事件

Dim n As Integer
    Dim i As Integer = lstPlanned.SelectedIndex

    If lstPlanned.SelectedItems.Count = 0 Then Exit Sub

    For n = 0 To UBound(AllDetails)

        If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lstPlanned.SelectedItem Then

            If Not My.Computer.FileSystem.DirectoryExists(zMailbox & AllDetails(n).uFile) Then

                MsgBox("No files located for " & vbNewLine & (AllDetails(n).uName & " (" & AllDetails(n).uCode) & ")" & vbNewLine & " " & vbNewLine & "Please try another...", MsgBoxStyle.OkOnly, "Lynx Control Panel")
                Exit Sub
            End If

            System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)

            For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
                If File.Exists(f) Then
                    File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                End If
            Next

            For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.ini", SearchOption.AllDirectories)
                If File.Exists(f) Then
                    File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                End If
            Next

            For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.txt", SearchOption.AllDirectories)
                If File.Exists(f) Then
                    File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                End If
            Next


            tvProgress.Nodes.Remove(rN)
            tvProgress.Nodes.Insert(0, rN)
            tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))


            If i >= 0 And i < lstPlanned.Items.Count Then
                lstPlanned.Items.RemoveAt(i)


            End If
            Exit Sub
        End If
    Next

【问题讨论】:

  • Node 是在运行时添加的,那么它应该如何在重启时出现?
  • 我的父节点使用DirectoryInfo 方法在重启时返回,但是子节点是已复制到父目录中的文件,这些文件需要在重启时作为父节点返回
  • 在你的程序中你从哪里开始填充树节点?新的,加载的...
  • 在程序启动时创建一个新的树节点,并定义了一个根节点。从另一个列表框中双击项目并将其复制到创建父节点的树视图。使用副本创建目录并复制某些文件。然后我们回到最初的问题。基本上我需要运行像DirectoryInfo 这样的过程并使用目录中的文件填充相关的父节点
  • 你能显示你填充 TreeNodeView 的部分吗?我会说,当您拥有每个文件夹的 DirectoryInfo 时,您可以使用 GetFiles 访问其中的文件并将其附加到添加的最后一个父节点。但也许我没听懂……

标签: vb.net treeview


【解决方案1】:

认为这是你想要的......也许;)

       ...

        System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)

        ' After the Dir is created Node is added to the TreeView
        tvProgress.Nodes.Remove(rN)
        tvProgress.Nodes.Insert(0, rN)
        tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))

        For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
            If File.Exists(f) Then
                File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)

                'As ParentNode already exists as Pos 0 you can add child nodes to it
                tvProgress.Nodes(0).Nodes.Add(Path.GetFileName(f))

            End If
        Next

        ...

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多