Results 1 to 11 of 11

Thread: [RESOLVED] TreeView Control

Threaded View

  1. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: TreeView Control

    This is the xml I used for testing:

    xml Code:
    1. <?xml version="1.0"?>
    2. <Root>
    3.     <FileTypes>
    4.         <FileType>TXT</FileType>
    5.         <FileType>DAT</FileType>
    6.         <FileType>ZIP</FileType>
    7.         <SomethingElse>
    8.             <FileType>BAT</FileType>
    9.             <FileType>EXE</FileType>
    10.         </SomethingElse>
    11.     </FileTypes>
    12. </Root>

    The first attached image is what it would normally do, the second is what the following code will do (I did change a few things, variable names, etc...):

    vb.net Code:
    1. Private Sub PopulateTreeView(ByVal fileName As String)
    2.  
    3.         Try
    4.             Dim document As XmlDocument = New XmlDocument()
    5.             document.Load(fileName)
    6.             Dim rootNode As New TreeNode(document.DocumentElement.Name)
    7.  
    8.             ' Clear out TreeView and add first (root) node
    9.             Me.ConfigTreeView.Nodes.Clear()
    10.             Me.ConfigTreeView.Nodes.Add(rootNode)
    11.  
    12.             ' Call AddTreeNode where I will add all nodes to the Tree
    13.             Me.AddTreeNodes(document.DocumentElement, rootNode)
    14.         Catch ex As Exception
    15.             MessageBox.Show(ex.Message)
    16.         End Try
    17.  
    18.     End Sub
    19.  
    20.     ' This method is called recursively until all nodes are loaded
    21.     Private Sub AddTreeNodes(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)
    22.  
    23.         Dim currentNode As XmlNode = Nothing
    24.  
    25.         ' If the current node has children, add those nodes as well
    26.         If (xmlNode.HasChildNodes) Then
    27.  
    28.             For index As Integer = 0 To xmlNode.ChildNodes.Count - 1
    29.  
    30.                 currentNode = xmlNode.ChildNodes(index)
    31.  
    32.                 If currentNode.Name = "FileType" Then
    33.                     For Each child As XmlNode In currentNode.ChildNodes
    34.                         treeNode.Nodes.Add(child.Value)
    35.                     Next
    36.                 ElseIf Not Object.Equals(currentNode.Value, treeNode.Text) Then
    37.                     treeNode.Nodes.Add(New TreeNode(currentNode.Name))
    38.                     Me.AddTreeNodes(currentNode, treeNode.Nodes(index))
    39.                 End If
    40.  
    41.             Next
    42.         End If
    43.  
    44.     End Sub

    Let me know if that works...
    Attached Images Attached Images   

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