VB.Net如何刷新TreeView NodeMouseClick中的节点

时间:2012-03-26 19:06:27

标签: vb.net treeview

我尝试使用TreeView编写部署工具。我在网上找到了几个教程,用于使用文件夹/子文件夹/文件填充树视图。一切正常,我处理文件部署的功能似乎还可以,但我有显示问题。

我的treeView正确显示我的文件夹结构和每个文件夹中的文件,甚至将正确的图标图像附加到每个文件夹/文件。

如果我单击+展开或折叠节点(文件夹),一切仍然正常,但如果我对文件夹执行单击,则会显示_NodeMouseClick事件正在触发,而不是正确刷新我的内容。不再显示任何子文件夹,文件现在具有文件夹图标。如果我崩溃并重新展开文件夹节点,一切都会以它应该的方式回归。

以下是相关代码:

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ' when our component is loaded, we initialize the TreeView by  adding  the root node
    Dim mRootNode As New TreeNode
    mRootNode.Text = RootPath
    mRootNode.Tag = RootPath
    mRootNode.ImageKey = CacheShellIcon(RootPath)
    mRootNode.SelectedImageKey = mRootNode.ImageKey
    mRootNode.Nodes.Add("*DUMMY*")
    TreeView1.Nodes.Add(mRootNode)

End Sub

Private Sub _BeforeCollapse(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
    ' clear the node that is being collapsed
    e.Node.Nodes.Clear()

    ' and add a dummy TreeNode to the node being collapsed so it is expandable again
    e.Node.Nodes.Add("*DUMMY*")
End Sub

Private Sub _BeforeExpand(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
    ' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
    e.Node.Nodes.Clear()

    AddImages(e)

End Sub

Private Sub _AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    e.Node.Nodes.Clear()
    Dim folder As String = CStr(e.Node.Tag)
    If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
        Try
            For Each file As String In IO.Directory.GetFiles(folder)
                e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub

我想我需要尝试从_NodeMouseClick例程调用AddImages例程,但是还没有能够使它工作。 AddImages接受TreeViewCancelEventArgs,我在_NodeMouseClick例程中没有。

    Private Sub AddImages(ByRef e As System.Windows.Forms.TreeViewCancelEventArgs)
    '---[ get the directory representing this node ]---
    Dim mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)

    '---[ add each subdirectory from the file system to the expanding node as a child node ]---
    For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
        '---[ declare a child TreeNode for the next subdirectory ]---
        Dim mDirectoryNode As New TreeNode
        '---[ store the full path to this directory in the child TreeNode's Tag property ]---
        mDirectoryNode.Tag = mDirectory.FullName
        '---[ set the child TreeNodes's display text ]---
        mDirectoryNode.Text = mDirectory.Name
        mDirectoryNode.ImageKey = CacheShellIcon(mDirectory.FullName)
        mDirectoryNode.SelectedImageKey = mDirectoryNode.ImageKey
        '---[ add a dummy TreeNode to this child TreeNode to make it expandable ]---
        mDirectoryNode.Nodes.Add("*DUMMY*")
        '---[ add this child TreeNode to the expanding TreeNode ]---
        e.Node.Nodes.Add(mDirectoryNode)
    Next

    '---[ add each file from the file system that is a child of the argNode that was passed in ]---
    For Each mFile As IO.FileInfo In mNodeDirectory.GetFiles
        '---[ declare a TreeNode for this file ]---
        Dim mFileNode As New TreeNode
        '---[ store the full path to this file in the file TreeNode's Tag property ]---
        mFileNode.Tag = mFile.FullName
        '---[ set the file TreeNodes's display text ]---
        mFileNode.Text = mFile.Name
        mFileNode.ImageKey = CacheShellIcon(mFile.FullName)
        mFileNode.SelectedImageKey = mFileNode.ImageKey & "-open"
        '---[ add this file TreeNode to the TreeNode that is being populated ]---
        e.Node.Nodes.Add(mFileNode)
    Next
End Sub

如果有人有任何提示,我将非常感谢帮助。 谢谢,

1 个答案:

答案 0 :(得分:1)

我猜这个问题就是这个:

Private Sub _AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
    e.Node.Nodes.Clear()
  '// etc
End Sub

它会删除您在BeforeExpand事件中添加的所有节点。