以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]树节点移除  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=160010)

--  作者:chnfo
--  发布时间:2021/1/17 16:43:00
--  [求助]树节点移除
Dim trv1 As WinForm.TreeView = Forms("XX").Controls("TreeView1")
Dim dt As DataTable = DataTables("AA")
dim txt as string = "ABCD"

Dim lstnd As new list(of WinForm.TreeNode)  \'需要保留的节点
For Each nd As WinForm.TreeNode In trv1.AllNodes
     Dim y As String = nd.FullPath & " " & py.GetPY(nd.FullPath,True,3)
     Dim yn As Boolean = False
     If Instr(y,txt) = 0 Then
          yn = True
     End If

     If yn = False Then   \'\'\'形成保留集合
        lstnd.add(nd)
     End If
Next


For Each nd As WinForm.TreeNode In trv1.AllNodes
   if lstnd.contains(nd) = false then
trv1.nodes.remove(nd)
   end if
Next

为什么执行的结果整个树全清了?

[此贴子已经被作者于2021/1/17 16:43:36编辑过]

--  作者:有点蓝
--  发布时间:2021/1/17 20:47:00
--  
很简单的道理,假设节点全路径是:a\\b\\c\\d,要保留节点d,abc也必须保留。但是abc肯定是不符合【 if lstnd.contains(nd) = false then】这个条件的,a删除后子节点bcd都会被一起删除了