Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:52:10+00:00 2026-05-31T22:52:10+00:00

I’m attempting to write a deployment tool using the TreeView. I followed a couple

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:52:11+00:00Added an answer on May 31, 2026 at 10:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.