The root element of the TreeView node is copied.

I have the following problem. I am browsing a TreeView to populate the XML tree with the elements marked in my TreeView. Basically, everything works fine, except that every time I finish populating the XML tree, I get a copy of my TreeView-rootnode inside the TreeView.

The strange thing is that the new node behaves like a ghost of the first. I cannot check / uncheck the boxes, but the corresponding fields in the source node are checked / unchecked. But I can expand or collapse the ghost nodes.

The number of my TreeView.Nodes also remains equal to 1, so removing a ghost is not possible since it does not exist. I also tried updating TreeView, but there were no changes. Even cleaning up TreeView does not eliminate the ghost (cleaning is also not the preferred option;)).

Here are the relevant code snippets:

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 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 

After completing the first call to buildReportTree, a ghost appears. Any ideas what might be the problem? Perhaps I have not yet found the correct search queries, but so far I have not found the answers to this.

Thanks a lot!

+4
source share
1 answer

From OP :

Hi everyone, I found the answer (the correct search query was "phantom"): the root of the node must be assigned to a variable, then it works. Like the original poster, I have no idea why. Here is the original forum post I found: Phantom Nodes

+1
source

All Articles