我有一个3层树视图,并使用下面的代码。我正在做以下事情,
1)如果选中parent,请检查所有子节点。
2)如果未选中一个子节点,请取消选中父节点。
3)如果选中了所有子节点,请检查父节点。
以下代码仅适用于2层。如果你可以指导我如何使它成为三个,将不胜感激。关于树视图有很多信息,但大多数信息似乎无关紧要= /如果你能给我一个很好的树视图控制指南,那将会很棒。谢谢!
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
答案 0 :(得分:6)
使用递归的工作示例:
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
答案 1 :(得分:1)
检查以下安静的简单代码。虽然它缺少您的一项要求,但它很轻松且令人满意。 VB.NET 2010。
Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
Dim childNodeCK As TreeNode = e.Node
Dim NodeChecked As Boolean = e.Node.Checked
For Each ChildNode As TreeNode In childNodeCK.Nodes
If NodeChecked = True Then
ChildNode.Checked = NodeChecked
End If
Next
If childNodeCK.Checked = False Then
If e.Node.Parent Is Nothing = False Then
e.Node.Parent.Checked = False
End If
End If
End Sub