VB.NET – TreeView’s root node is copied

I have the following problem. I am using TreeView to populate the XML tree, which contains the items checked in the TreeView. Basically everything is working fine, except every time I fill in the XML tree, I Will get a copy of TreeView-rootnode in TreeView.

The strange thing is that the new node behaves like the ghost of the first node. I can’t check/uncheck the checkbox, but Will check/uncheck the corresponding box in the original node. But I am able to expand or collapse the ghost’s node.

The count of my TreeView.Nodes also remains at 1, so deleting the ghost is impossible because It’s not there. I also tried refreshing the TreeView, but nothing changed. Even clearing the TreeView will not eliminate the ghosting (clearing is not the preferred option either ;)).

Here is the relevant code snippet:

p>

Private Sub btnSaveReport_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveReport.Click
Dim newXML As XDocument = XDocument.Load("some.xml" )
Dim xmlTree As XElement = newXML.Root
buildReportTree(trvMyTree.Nodes(0), xmlTree)
Console.WriteLine(xmlTree)
End Sub

Private Sub buildReportTree(ByRef treeNode As TreeNode, ByRef currentElement As XElement)
If treeNode.Checked Then
Dim newNode As XElement
newNode = buildReportNode(treeNode)
currentElement.Add (newNode)
For Each childNode As TreeNode In tre eNode.Nodes
buildReportTree(childNode, newNode)
Next
End If
End Sub

Private Function buildReportNode(treeNode As TreeNode) As XElement
If treeNode.ToolTipText = "property" Then
Dim newNode As XElement = New XElement(treeNode.ToolTipText, treeNode.Name)
Return newNode
End If
If treeNode. ToolTipText = "collection" Or treeNode.ToolTipText = "reference" Then
Dim newNode As XElement = New XElement(treeNode.ToolTipText, _
New XAttribute("name", treeNode.Name))
Return newNode
End If
Return Nothing 'ToDo: handle errors
End Function

Once the first call to buildReportTree is completed, ghosting will appear. Any ideas may be what is the problem? Maybe I haven’t found a suitable search term, but I haven’t found any answers so far.

Thank you very much!

From the OP:

Hi everyone, I found the answer (correct search term was “phantom”): The root node has to be assigned to a variable, then it works. As the original poster, I have no idea why. Here’s the original forum post I found: 07001

I have the following problem. I am populating an XML tree through TreeView, which contains items checked in TreeView. Basically Everything is working fine, except every time I fill in the XML tree, I get a copy of the TreeView-rootnode in the TreeView.

The strange thing is that the new node behaves like the first node The same as the ghost. I can’t check/uncheck the checkbox, but will check/uncheck the corresponding box in the original node. But I can expand or collapse the ghost’s nodes.

My TreeView.Nodes count It also stays at 1, so it is impossible to delete the ghost because it is not there. I have also tried refreshing the TreeView, but no change. Even clearing the TreeView will not eliminate the ghosting (clearing is not the preferred option either ;)).

This is the relevant code snippet:

Private Sub btnSaveReport_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveReport.Click
Dim newXML As XDocument = XDocument.Load("some.xml")
Dim xmlTree As XElement = newXML.Root
buildReportTree(trvMyTree.Nodes(0), xmlTree)
Console.WriteL ine(xmlTree)
End Sub

Private Sub buildReportTree(ByRef treeNode As TreeNode, ByRef currentElement As XElement)
If treeNode.Checked Then
Dim newNode As XElement< br /> newNode = buildReportNode(treeNode)
currentElement.Add(newNode)
For Each childNode As TreeNode In treeNode.Nodes
buildReportTree(childNode, newNode)
Next
End If
End Sub

Private Function buildReportNode(treeNode As TreeNode) As XElement
If treeNode.ToolTipText = "property" Then
Dim newNode As XElement = New XElement(treeNode.ToolTipText, treeNode.Name)
Return newNode
End If
If treeNode.ToolTipText = "collection" Or treeNode.ToolTipText = "reference" Then
Dim newNode As XElement = New XElement(treeNode.ToolTipText, _
New XAttribute("name", treeNode.Name))
Return newNode
End If
Return Nothing 'ToDo: handle errors
End Function

Once the first call to buildReportTree is completed, ghosting will appear. Any ideas what might be the problem? Maybe I haven’t found a suitable search term, but I haven’t found any answers so far.

Thank you very much!

From the OP:

Hi everyone, I found the answer ( correct search term was “phantom”): The root node has to be assigned to a variable, then it works. As the original poster, I have no idea why. Here’s the original forum post I found: 07001

Leave a Comment

Your email address will not be published.