检测树视图何时加载节点?

时间:2018-06-27 09:06:56

标签: vb.net treeview loading

我的应用程序使用树形视图从文件夹加载文件目录。但是根据文件夹的不同,加载树视图可能需要一段时间,因此我的程序似乎冻结了,并且树视图在一段时间内为空白,但事实并非如此。加载较大的文件夹只需要较长的时间。但我想显示一个带有动画GIF的图片框,以使用户知道一切正常,他们只需要等待即可。问题是,我似乎找不到发生这种情况的事件。我已竭尽所能,甚至在Google上搜索了无法成功的答案。任何帮助,将不胜感激。谢谢。

代码:

 Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)

  picturebox3.visible = true 

        Dim folder As String = String.Empty
        Try
            'Add folders to treeview
            Dim folders() As String = IO.Directory.GetDirectories(dir)
            If folders.Length <> 0 Then
                Dim folderNode As TreeNode = Nothing
                Dim folderName As String = String.Empty
                For Each folder In folders
                    folderName = IO.Path.GetFileName(folder)
                    folderNode = parentNode.Nodes.Add(folderName)
                    folderNode.Tag = "folder"
                    folderNode.Name = "folder"
                    PopulateTreeView(folder, folderNode)
                Next
            End If

            'Add the files to treeview
            Dim files() As String = IO.Directory.GetFiles(dir)
            TreeView1.Nodes(0).Tag = "folder"
            TreeView1.Nodes(0).Name = "folder"
            If files.Length <> 0 Then
                Dim fileNode As TreeNode = Nothing
                For Each file As String In files
                    fileNode = parentNode.Nodes.Add(IO.Path.GetFileName(file))
                    fileNode.Tag = "file"
                    If file.Contains(".html") Or file.Contains(".htm") Or file.Contains(".HTML") Or file.Contains(".HTM") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(1)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(1)
                        fileNode.Name = "html"
                    ElseIf file.Contains(".css") Or file.Contains(".CSS") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(2)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(2)
                        fileNode.Name = "css"
                    ElseIf file.Contains(".js") Or file.Contains(".JS") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(3)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(3)
                        fileNode.Name = "js"
                    ElseIf file.Contains(".php") Or file.Contains(".PHP") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(4)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(4)
                        fileNode.Name = "php"
                    ElseIf file.Contains(".png") Or file.Contains(".PNG") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(5)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(5)
                        fileNode.Name = "png"
                    ElseIf file.Contains(".bmp") Or file.Contains(".BMP") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(6)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(6)
                        fileNode.Name = "bmp"
                    ElseIf file.Contains(".gif") Or file.Contains(".GIF") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(7)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(7)
                        fileNode.Name = "gif"
                    ElseIf file.Contains(".jpg") Or file.Contains(".jpeg") Or file.Contains(".JPG") Or file.Contains(".JPEG") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(8)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(8)
                        fileNode.Name = "jpg"
                    ElseIf file.Contains(".txt") Or file.Contains(".TXT") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(9)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(9)
                        fileNode.Name = "txt"
                    ElseIf file.Contains(".ttf") Or file.Contains(".TTF") Or file.Contains(".otf") Or file.Contains(".OTF") Then
                        fileNode.ImageKey = ImageList1.Images.Keys(10)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(10)
                        fileNode.Name = "font"
                    Else
                        fileNode.ImageKey = ImageList1.Images.Keys(11)
                        fileNode.SelectedImageKey = ImageList1.Images.Keys(11)
                        fileNode.Name = "other"
                    End If
                Next
            End If

        Catch ex As UnauthorizedAccessException
            parentNode.Nodes.Add("Access Denied")
        End Try

2 个答案:

答案 0 :(得分:0)

您可以使用Async / Await模式在不冻结UI的情况下在其他线程中加载数据,然后将数据添加到树中。

另外,当将节点添加到树中时,请使用AddRange或一个一个地添加,请在第一个节点之前调用BeginUpdate,在最后一个节点之后调用EndUpdate。例如,您可以看到如何在不冻结UI的情况下在另一个线程中进行5秒的虚拟延迟来添加两个节点:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Await AddNodes()
End Sub

Private Async Function AddNodes() As Task
    PictureBox1.Show()
    PictureBox1.Update()
    Await Task.Delay(5000)
    Dim Nodes = New List(Of TreeNode)() From {New TreeNode("1"), New TreeNode("2")}
    TreeView1.Nodes.AddRange(Nodes.ToArray())
    PictureBox1.Hide
End Function

答案 1 :(得分:0)

首先不要使用后台工作程序try任务,线程或异步

我稍微升级并添加onExpand事件,因此您无需一次运行就加载所有hardrive。展开后的每个根

也有Threat的示例,当您通过威胁进行更新时,如果没有交叉威胁,则需要使用Invoke。不保存在100%。你有基础

Private Sub TreeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
    Dim tr = New Threading.Thread(AddressOf ExpandNode)
    tr.Start(e.Node)
End Sub
Sub UpNode(e As TreeNode, onNod As TreeNode())
    e.Nodes.Clear()
    e.Nodes.AddRange(onNod)
End Sub
Public Sub ExpandNode(e As TreeNode)
    Dim l = PopulateTreeView(e.Tag)
    Me.Invoke(Sub() UpNode(e, l))
End Sub

Private Function PopulateTreeView(ByVal dir As String) As TreeNode()
    Dim Li As New List(Of TreeNode)
    Dim folder As String = String.Empty
    Try
        'Add folders to treeview
        Dim folders() As String = IO.Directory.GetDirectories(dir)
        If folders.Length <> 0 Then
            For Each folder In folders
                Dim Ndir = New TreeNode
                Ndir.Tag = folder
                Ndir.Text = System.IO.Path.GetFileName(folder)
                Ndir.Nodes.Add(".")
                Li.Add(Ndir)
            Next
        End If

        'Add the files to treeview
        Dim files() As String = IO.Directory.GetFiles(dir)
        If files.Length <> 0 Then
            Dim fileNode As TreeNode = New TreeNode
            For Each file As String In files
                fileNode.Tag = file
                fileNode.Text = System.IO.Path.GetFileName(file)
                If file.Contains(".html") Or file.Contains(".htm") Or file.Contains(".HTML") Or file.Contains(".HTM") Then
                    fileNode.Name = "html"
                ElseIf file.Contains(".css") Or file.Contains(".CSS") Then
                    fileNode.Name = "css"
                ElseIf file.Contains(".js") Or file.Contains(".JS") Then
                    fileNode.Name = "js"
                ElseIf file.Contains(".php") Or file.Contains(".PHP") Then
                    fileNode.Name = "php"
                ElseIf file.Contains(".png") Or file.Contains(".PNG") Then
                    fileNode.Name = "png"
                ElseIf file.Contains(".bmp") Or file.Contains(".BMP") Then
                    fileNode.Name = "bmp"
                ElseIf file.Contains(".gif") Or file.Contains(".GIF") Then
                    fileNode.Name = "gif"
                ElseIf file.Contains(".jpg") Or file.Contains(".jpeg") Or file.Contains(".JPG") Or file.Contains(".JPEG") Then
                    fileNode.Name = "jpg"
                ElseIf file.Contains(".txt") Or file.Contains(".TXT") Then
                    fileNode.Name = "txt"
                ElseIf file.Contains(".ttf") Or file.Contains(".TTF") Or file.Contains(".otf") Or file.Contains(".OTF") Then
                    fileNode.Name = "font"
                Else
                    fileNode.Name = "other"
                End If
            Next
            Li.Add(fileNode)
        End If

    Catch ex As UnauthorizedAccessException
        Li.Add(New TreeNode With {.Tag = Nothing, .Name = "Access Denied"})
    End Try
    Return Li.ToArray
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Node = New TreeNode
    Node.Tag = "C:\"
    Node.Text = "C:"
    Node.Nodes.Add(".")
    TreeView1.Nodes.Add(Node)
End Sub