tkflick
tkflick

Reputation: 13

VB.Net How to refresh node in TreeView NodeMouseClick

I'm attempting to write a deployment tool using the TreeView. I followed a couple of tutorials I found online for populating the treeview with folders/subfolders/and files. That all works and my functionality for processing my file deployment seems to be ok, but I'm having a display issue.

My treeView displays my folder structure and the files inside each folder properly, even attaching the correct icon image to each folder / file.

If I click the + to expand or collapse a node (folder) everything is still fine, but if I perform a single click on a folder, it appears the _NodeMouseClick event is firing and not refreshing my contents correctly. Any subfolders are no longer displayed, and the files now have the folder icon. If I collapse and re-expand the folder node, everything goes back the way it should.

Here's the relevant code:

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

I think I need to try and call the AddImages routine from the _NodeMouseClick routine, but haven't been able to make it work. AddImages accepts TreeViewCancelEventArgs and I don't have that in the _NodeMouseClick routine.

    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

If anyone has any tips, I would greatly appreciate the help. Thanks,

Upvotes: 1

Views: 5433

Answers (1)

LarsTech
LarsTech

Reputation: 81610

I'm guessing the problem is with this:

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

It deletes all of the nodes you are adding in the BeforeExpand event.

Upvotes: 1

Related Questions