以文本方式查看主题

-  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=86902)

--  作者:lzzhx
--  发布时间:2016/6/28 16:39:00
--  [求助] 树节点删除问题
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim nd As WinForm.TreeNode
trv.Nodes.Clear
trv.Nodes.Add("按钮","按钮")
trv.Nodes.Add("数据表","数据表")
trv.Nodes.Add("单选框","单选框")
trv.Nodes.Add("复选框","复选框")
nd = trv.Nodes(0)
nd.nodes.Add("按钮1","按钮1")
nd = trv.Nodes(1)
nd.nodes.Add("数据表1","数据表1")
For Each nd In trv.Nodes
    MessageBox.Show(nd.text & "=" & nd.Nodes.Count )
    If nd.Nodes.Count = 0 Then
        nd.Delete
    End If
Next
上面代码中树节点遍历从消息框看根本就到不了第4个节点,要把红色的代码注释掉,遍历树节点就正常,现在想判断一下当树节点没有子节点时应把该节点删除,该如何写代码?

--  作者:大红袍
--  发布时间:2016/6/28 16:44:00
--  
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim nd As WinForm.TreeNode
trv.Nodes.Clear
trv.Nodes.Add("按钮","按钮")
trv.Nodes.Add("数据表","数据表")
trv.Nodes.Add("单选框","单选框")
trv.Nodes.Add("复选框","复选框")
nd = trv.Nodes(0)
nd.nodes.Add("按钮1","按钮1")
nd = trv.Nodes(1)
nd.nodes.Add("数据表1","数据表1")
Dim ls As new List(of object)
For Each nd In trv.Nodes
    MessageBox.Show(nd.text & "=" & nd.Nodes.Count )
    If nd.Nodes.Count = 0 Then
        ls.add(nd)
    End If
Next
For Each nd In ls
    nd.delete
next

--  作者:lzzhx
--  发布时间:2016/6/28 16:46:00
--  
谢谢