Results 1 to 11 of 11

Thread: [RESOLVED] TreeView Control

  1. #1

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Resolved [RESOLVED] TreeView Control

    I'm have an xml document populating a treeview that looks something like this:

    Root
    Filetypes
    Filetype
    TXT
    Filetype
    DAT
    Filetype
    ZIP


    That's correct based on the xml document, but I want to display it like this:

    Root
    Filetype
    TXT
    DAT
    ZIP

    I'm not sure how to start on this. Do you know what I might do? Thanks

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

    Re: TreeView Control

    So you only want unique node names?

  3. #3

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Question Re: TreeView Control

    Quote Originally Posted by ForumAccount View Post
    So you only want unique node names?
    Yes that's right. Also, if you didn't want to show some of the child nodes how would you do that?

    Thanks a lot

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

    Re: TreeView Control

    The ideal starting point is to show the code you have that is loading the TreeView.

  5. #5

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: TreeView Control

    Quote Originally Posted by ForumAccount View Post
    The ideal starting point is to show the code you have that is loading the TreeView.
    Ok:

    Code:
     Private Sub PopulateTreeView(ByVal FileName As String)
    
            Try
                ' Create a new instance of the XmlDocument class
                Dim xDoc As XmlDocument = New XmlDocument
    
                ' Load XML Document
                xDoc.Load(FileName)
    
                ' Clear out TreeView and add first (root) node
                ConfigTreeView.Nodes.Clear()
    
    
                ConfigTreeView.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
    
                ' Create a new instance of the TreeNode class
                Dim tNode As TreeNode = New TreeNode()
    
                ' Set tNode equal to the first node in the tree
                tNode = ConfigTreeView.Nodes(0)
    
                ' Call AddTreeNode where I will add all nodes to the Tree
                AddTreeNode(xDoc.DocumentElement, tNode)
    
                ' Expand TreeView to show all nodes (probably want to take this out when finished)
                'ConfigTreeView.ExpandAll()
    
                ' xml exception
            Catch xmlEx As XmlException
                MessageBox.Show(xmlEx.Message)
    
                ' general exception
            Catch Ex As Exception
                MessageBox.Show(Ex.Message)
            End Try
        End Sub
    
        ' This method is called recursively until all nodes are loaded
        Private Sub AddTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)
    
            Dim xNode As XmlNode
            Dim tNode As TreeNode
            Dim XNodeList As XmlNodeList
    
            ' If the current node has children, add those nodes as well
            If (xmlNode.HasChildNodes) Then
    
                XNodeList = xmlNode.ChildNodes
    
                For x As Integer = 0 To XNodeList.Count - 1
    
                    xNode = xmlNode.ChildNodes(x)
    
    
                    treeNode.Nodes.Add(New TreeNode(xNode.Name))
                    tNode = treeNode.Nodes(x)
                    AddTreeNode(xNode, tNode)  ' Recursive call. Keep calling self all nodes are loaded
                Next
    
                ' Otherwise there are no children so add the outer xml, trimming off whitespace
            Else
                treeNode.Text = xmlNode.OuterXml.Trim()
            End If
    
        End Sub

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

    Re: TreeView Control

    So you have something similar to the root's first node, but you want it like the root's second node:
    Attached Images Attached Images  
    Last edited by ForumAccount; Oct 7th, 2009 at 03:07 PM. Reason: Attached Image Instead

  7. #7

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: TreeView Control

    Quote Originally Posted by ForumAccount View Post
    So you have something similar to the root's first node, but you want it like the root's second node:

    I will have to look at that at home. That site is blocked at work.

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

    Re: TreeView Control

    I attached it instead

  9. #9

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Re: TreeView Control

    Quote Originally Posted by ForumAccount View Post
    I attached it instead
    Thanks. That's exactly what I'm looking for.

  10. #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   

  11. #11

    Thread Starter
    Addicted Member rkeslar's Avatar
    Join Date
    Jul 2009
    Location
    Pittsburgh, PA
    Posts
    145

    Thumbs up Re: TreeView Control

    Quote Originally Posted by ForumAccount View Post
    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...
    Wow that's great. I will work on it now. Thanks a lot!

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