gpt4 book ai didi

vb.net - 3 层 Treeview 节点 Checkboxes

转载 作者:行者123 更新时间:2023-12-01 15:02:15 25 4
gpt4 key购买 nike

我有一个 3 层 TreeView 并使用下面的代码。我正在让它执行以下操作,

1)如果父节点被选中,则检查所有子节点。

2)如果只有一个子节点未勾选,则取消勾选父节点。

3) 如果所有的子节点都被选中,则检查父节点。

下面的代码只适用于 2 层。如果你能指导我如何让它成为三个,我将不胜感激。有很多关于 treeview 的信息,但其中大部分似乎无关紧要 =/如果你能给我一个关于 treeview 控件的好指南,那就太好了。谢谢!

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles TreeView1.AfterCheck
RemoveHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck

For Each node As TreeNode In e.Node.Nodes
node.Checked = e.Node.Checked
Next

If e.Node.Checked Then
If e.Node.Parent Is Nothing = False Then
Dim allChecked As Boolean = True

For Each node As TreeNode In e.Node.Parent.Nodes
If Not node.Checked Then
allChecked = False
End If
Next

If allChecked Then
e.Node.Parent.Checked = True
End If

End If
Else
If e.Node.Parent Is Nothing = False Then
e.Node.Parent.Checked = False
End If
End If

AddHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck
End Sub

最佳答案

使用递归的工作示例:

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For i As Integer = 1 To 3
Dim node As New TreeNode("Parent #" & i.ToString)
node.Nodes.Add("Child #1")
node.Nodes.Add("Child #2")

Dim childNode As New TreeNode("Child #3")
childNode.Nodes.Add("Grand Child #1")
childNode.Nodes.Add("Grand Child #2")

Dim grandNode As New TreeNode("Grand Child #3")
grandNode.Nodes.Add("Great Grand Child #1")
grandNode.Nodes.Add("Great Grand Child #2")
childNode.Nodes.Add(grandNode)
node.Nodes.Add(childNode)

grandNode.Expand()
childNode.Expand()
node.Expand()

TreeView1.Nodes.Add(node)
Next
End Sub

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles TreeView1.AfterCheck
RemoveHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck

Call CheckAllChildNodes(e.Node)

If e.Node.Checked Then
If e.Node.Parent Is Nothing = False Then
Dim allChecked As Boolean = True
Call IsEveryChildChecked(e.Node.Parent, allChecked)
If allChecked Then
e.Node.Parent.Checked = True
Call ShouldParentsBeChecked(e.Node.Parent)
End If
End If
Else
Dim parentNode As TreeNode = e.Node.Parent
While parentNode Is Nothing = False
parentNode.Checked = False
parentNode = parentNode.Parent
End While
End If

AddHandler TreeView1.AfterCheck, AddressOf TreeView1_AfterCheck
End Sub

Private Sub CheckAllChildNodes(ByVal parentNode As TreeNode)
For Each childNode As TreeNode In parentNode.Nodes
childNode.Checked = parentNode.Checked
CheckAllChildNodes(childNode)
Next
End Sub

Private Sub IsEveryChildChecked(ByVal parentNode As TreeNode, ByRef checkValue As Boolean)
For Each node As TreeNode In parentNode.Nodes
Call IsEveryChildChecked(node, checkValue)
If Not node.Checked Then
checkValue = False
End If
Next
End Sub

Private Sub ShouldParentsBeChecked(ByVal startNode As TreeNode)
If startNode.Parent Is Nothing = False Then
Dim allChecked As Boolean = True
Call IsEveryChildChecked(startNode.Parent, allChecked)
If allChecked Then
startNode.Parent.Checked = True
Call ShouldParentsBeChecked(startNode.Parent)
End If
End If
End Sub

End Class

关于vb.net - 3 层 Treeview 节点 Checkboxes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com