在 Visual Basic 中检查父节点下的所有项目

发布于 2024-09-06 17:00:55 字数 1155 浏览 1 评论 0原文

我正在尝试检查父节点下的所有子节点,到目前为止,我的代码仅在 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

I am trying to check all the childnodes under a parent node, the code I have so far only goes about 2-3 levels deep in the TreeView and I am looking to grab all nodes no matter how deep they are. Could someone shed some insight on this.

Below is the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-09-13 17:00:55

为了获得所有子节点,无论它们有多深,你肯定需要递归。

也就是说,您获取节点的每个直接子节点并对该节点进行检查。您还可以调用该方法来检查每个子节点的子节点。

所以这里是伪代码。

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

这里是 vb.net 的递归教程。 这里此处您可以在此处找到更多信息

In order to get all subnodes no matter how deep they are, you definitely need recursion.

That is, you take each direct child of a node and do the check for this node. And you also call the method to check subnodes for each child.

So here in Pseudocode.

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

Here's a recursion tutorial for vb.net. Here, here and here is you can find additional information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文