当前位置 : 主页 > 编程语言 > c语言 >

vb.net – TreeView的根节点被复制

来源:互联网 收集:自由互联 发布时间:2021-06-24
我有以下问题.我正在通过TreeView来填充 XML树,其中包含在TreeView中检查的项目.基本上一切都运行正常,除了每次填写XML树时,我都会在TreeView中获得TreeView-rootnode的副本. 奇怪的是,新节点的
我有以下问题.我正在通过TreeView来填充 XML树,其中包含在TreeView中检查的项目.基本上一切都运行正常,除了每次填写XML树时,我都会在TreeView中获得TreeView-rootnode的副本.

奇怪的是,新节点的行为就像第一个节点的幽灵一样.我无法选中/取消选中复选框,但会检查/取消选中原始节点中的相应框.但我能够扩展或折叠幽灵的节点.

我的TreeView.Nodes的计数也保持为1,因此删除鬼是不可能的,因为它不在那里.我也试过刷新TreeView,但没有变化.即使清除TreeView也不会消除重影(清除也不是首选选项;)).

这是相关的代码片段:

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

一旦第一次调用buildReportTree完成,就会出现重影.任何想法可能是什么问题?也许我还没找到合适的搜索词,但到目前为止我没有找到任何答案.

非常感谢!

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

网友评论