[英]check all items under a parent node in Visual Basic
我正在尝试检查父节点下的所有子节点,到目前为止,我的代码在TreeView中仅深入2-3层,并且无论它们有多深,我都希望抓住所有节点。 有人可以对此有所了解。
下面是代码:
Private Sub CheckChildNode(ByVal currNode As TreeNode)
'set the children check status to the same as the current node
Dim checkStatus As Boolean = currNode.Checked
For Each node As TreeNode In currNode.Nodes
node.Checked = checkStatus
CheckChildNode(node)
Next
End Sub
Private Sub CheckParentNode(ByVal currNode As TreeNode)
Dim parentNode As TreeNode = currNode.Parent
If parentNode Is Nothing Then Exit Sub
parentNode.Checked = True
For Each node As TreeNode In parentNode.Nodes
If Not node.Checked Then
parentNode.Checked = False
Exit For
End If
Next
CheckParentNode(parentNode)
End Sub
Private Sub treeview_AfterCheck(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles treeview.AfterCheck
RemoveHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
CheckChildNode(e.Node)
CheckParentNode(e.Node)
AddHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
End Sub
为了获得所有子节点,无论它们的深度如何,您都绝对需要递归。
也就是说,您将节点的每个直接子节点作为节点进行检查。 并且您还调用该方法来检查每个子节点的子节点。
所以这里是伪代码。
node getParentNode(node child){
return child.parent
}
node checkNode(node n){
// perform check here for node n
if n is not valid {
return false!
}
// do children recursively
for each child in n.children{
// function calls itself!
checkNode(child)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.