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

  • Home
  • SEARCH
  • 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 929213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:07:58+00:00 2026-05-15T20:07:58+00:00

I want to be able to create a tree view that can get its

  • 0

I want to be able to create a tree view that can get its nodes form a directory on a computer. In the code below, I am able to get all of the files into a list, but I cannot get the folder correct. What I mean is in your user directory, you have sub directorys such as, Documents, Music, and Pictures. When you run this code, it displays them each as thier own node, not nested. I hope this makes sense. Thanks In VB.NET please.

     Private Sub PopulateTree(ByVal path As String, ByVal subfoldercount As Integer)

         Dim items() As String
         items = Directory.GetFileSystemEntries(path)
         Dim itm As String

         TreeVeiw1.Nodes.Add(path)
         Dim currentnode As TreeNode = TreeView1.Nodes.Item(0)
         For Each itm In items
             If Directory.Exists(itm) Then
                 Dim nodeOjb As New TreeNode
                 nodeOjb.Text = "FOLDER :: " & subfoldercount & " :: " & itm
                 nodeOjb.ForeColor = Color.Blue
                 currentnode.Nodes.Add(nodeOjb)
                 PopulateTree(itm, subfoldercount + 1)
             Else
                 Dim nodeOjb As New TreeNode
                 nodeOjb.Text = "FILE :: " & subfoldercount & " :: " & itm
                 Select Case My.Computer.FileSystem.GetFileInfo(itm).Extension
                     Case ".txt"
                         nodeOjb.ForeColor = Color.Orange
                         currentnode.Add(nodeOjb)
                     Case ".png"
                         nodeOjb.ForeColor = Color.Red
                         currentnode.Add(nodeOjb)
                    Case ".ico"
                         nodeOjb.ForeColor = Color.Green
                         currentnode.Add(nodeOjb)
                    Case ".url"
                         nodeOjb.ForeColor = Color.Black
                         currentnode.Add(nodeOjb)
                 End Select
             End If
         Next

     End Sub

Changed the code to the way spinion told me to. When I run this code I get an error ‘Object reference not set to an instance of an object’ when it starts to try and add files to the tree view.

  • 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-15T20:07:59+00:00Added an answer on May 15, 2026 at 8:07 pm

    Just from a quick glance it seems that you are adding every node you find to the root level of the tree.

    TreeView1.Nodes.Add(nodeOjb)
    

    What you should be doing is passing through with the recursive call the current node you are working on and use that to add the next level of nodes found.

    currentNode.Add(nodeObj)
    

    This way you can have children added to nodes. Rather than always adding all nodes to the root of the tree.

    EDIT: Here is the changes you need to make to the PopulateTree method:

    Private Sub PopulateTree(currentNode As TreeNode, path__1 As String, subfoldercount As Integer)
        Dim items As String() = Nothing
        items = Directory.GetFileSystemEntries(path__1)
    
        Dim nodeParent As TreeNodeCollection = If((currentNode IsNot Nothing), currentNode.Nodes, Me.TreeView1.Nodes)
    
        For Each itm As String In items
            If Directory.Exists(itm) Then
                Dim nodeOjb = New TreeNode()
                nodeOjb.Text = "FOLDER :: " & subfoldercount & " :: " & itm
                nodeOjb.ForeColor = Color.Blue
                nodeParent.Add(nodeOjb)
                Me.PopulateTree(nodeOjb, itm, subfoldercount + 1)
            Else
                Dim nodeOjb = New TreeNode()
                nodeOjb.Text = "FILE :: " & subfoldercount & " :: " & itm
                Select Case Path.GetExtension(itm)
                    Case ".txt"
                        nodeOjb.ForeColor = Color.Orange
                        nodeParent.Add(nodeOjb)
                        Exit Select
                    Case ".png"
                        nodeOjb.ForeColor = Color.Red
                        nodeParent.Add(nodeOjb)
                        Exit Select
                    Case ".ico"
                        nodeOjb.ForeColor = Color.Green
                        nodeParent.Add(nodeOjb)
                        Exit Select
                    Case ".url"
                        nodeOjb.ForeColor = Color.Black
                        nodeParent.Add(nodeOjb)
                        Exit Select
                End Select
            End If
        Next
    End Sub
    

    Then when you call it for the first time you do so like this:

    PopulateTree(Nothing, "", 0)
    

    You pass a empty reference for the first call so it uses the parent. The second parameter is your path.

    p.s. I am primarily a C# guy so I used a converter to change the code over. It should work but you might need to tweak a little.

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

Sidebar

Related Questions

I want to create a tree that it's child nodes contain specific flex components
I'm using jstree to create a multi-level tree, and I want to be able
I want to create a tree view which list down all properties in an
I want to be able to create a Pandas DataFrame with MultiIndexes for the
I want to be able to create links looking like buttons with dynamic text
I want to be able to create modal dialogs, with, for example close: function()
I want to be able to create a temporary text File in Java to
I want to be able to create the XSD file for my typed dataset
I want to be able to create a KPI List on my MOSS 2007
I want to be able to create a file from the Content Provider, however

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.