Results 1 to 3 of 3

Thread: TreeNodes Made EZ

Threaded View

  1. #1

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    TreeNodes Made EZ

    Came up with some simple to use treenode subs that simply require 2 simple things:
    1. a path, usually as a computer path, but you could easily trick it out and datatable paths (it would require a few very simple changes everywhere you see Directory.GetDirectories)
    2. the second sub simply needs to be placed in the treeview's BeforeExpand event

    Everything is broke down so it is very easy to follow and see what's going on, i'll add comments after lunch if i get a chance, i'm basically killing time for coffee to finish making for now.

    Simply add the first sub to your form load event with:
    Form Load Event Code:
    1. populateTreeView(yourTreeView, yourPath)
    First Sub:
    Populate TreeView Code:
    1. Public Sub populateTreeView(ByVal _treeView As TreeView, ByVal _path As String)
    2.         Try
    3.  
    4.             For Each dirParent As String In Directory.GetDirectories(_path)
    5.                 Dim parentNode As New TreeNode(dirParent.Substring(_path.Length + 1))
    6.                 trvMain.Nodes.Add(parentNode)
    7.  
    8.                 Try
    9.  
    10.                     'Works for Music/Pics/Video Folders in Windows 7/Vista
    11.                     If dirParent.Contains("Documents") Then
    12.                         If dirParent.Contains("Music") Then
    13.                             dirParent = dirParent.Replace("Documents\My ", "")
    14.                         End If
    15.                         If dirParent.Contains("Pictures") Then
    16.                             dirParent = dirParent.Replace("Documents\My ", "")
    17.                         End If
    18.                         If dirParent.Contains("Videos") Then
    19.                             dirParent = dirParent.Replace("Documents\My ", "")
    20.                         End If
    21.                     End If
    22.  
    23.                     For Each dirChild As String In Directory.GetDirectories(dirParent)
    24.  
    25.                         Dim path As String
    26.  
    27.                         Try
    28.  
    29.                             path = dirParent
    30.                             Dim childNode As New TreeNode(dirChild.Substring(dirParent.Length + 1))
    31.                             parentNode.Nodes.Add(childNode)
    32.  
    33.                         Catch ex As Exception
    34.                             Dim exMessage As String = ex.Message & Environment.NewLine
    35.                             'RelayException
    36.                         End Try
    37.  
    38.                     Next
    39.  
    40.                 Catch ex As Exception
    41.                     Dim exMessage As String = ex.Message & Environment.NewLine
    42.                      'RelayException
    43.                 End Try
    44.  
    45.             Next
    46.  
    47.         Catch ex As Exception
    48.             Dim exMessage As String = ex.Message & Environment.NewLine
    49.              'RelayException
    50.         End Try
    51.     End Sub


    Simply add the second sub to your treeview BeforeExpand event with:
    Treeview BeforeExpand Event Code:
    1. populateSubTreeView(e.node, yourPath)
    Second Sub:
    Populate Sub Tree View Code:
    1. Public Sub populateSubTreeView(ByVal eNode As TreeNode, ByVal _path As String)
    2.         Try
    3.  
    4.             Dim newPath As String = _path
    5.             If Not eNode.FullPath Is Nothing Then
    6.                 newPath = String.Concat(newPath, "\", eNode.FullPath)
    7.  
    8.                 Try
    9.  
    10.                     For Each child In eNode.Nodes
    11.                         Dim subNode As String = child.FullPath.Substring(eNode.FullPath.Length + 1)
    12.                         Dim subPath As String = String.Concat(newPath, "\", subNode)
    13.  
    14.                         Try
    15.  
    16.                             For Each folder In Directory.GetDirectories(subPath)
    17.                                 Dim childNode As New TreeNode(folder.Substring(subPath.Length + 1))
    18.                                 child.Nodes.Add(childNode)
    19.                             Next
    20.  
    21.                         Catch ex As Exception
    22.                             Dim exMessage As String = ex.Message
    23.                             'RelayException
    24.                         End Try
    25.  
    26.                     Next
    27.  
    28.                 Catch ex As Exception
    29.                     Dim exMessage As String = ex.Message
    30.                     'RelayException
    31.                 End Try
    32.  
    33.             End If
    34.  
    35.         Catch ex As Exception
    36.             Dim exMessage As String = ex.Message
    37.             'RelayException
    38.         End Try
    39.     End Sub

    HINT: use ctrl+f to replace all the 'RelayException marks with your own way (exp. Console.WriteLine(exMessage))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width