我也刚好碰到这个问题,昨晚在muhua版主的代码中找到了其中的逻辑,做好了5级筛选代码,但是这个代码太笨重了,我不懂得简化,请高手来指点简化一下代码。
Dim trv As WinForm.TreeView = e.Sender
Dim flt As String
For Each nd As WinForm.TreeNode In e.Node.allNodes '同步子节点选中状态。
nd.Checked = e.node.checked
Next
‘接下来的代码是针对父节点的。
If e.node.level = 1 Then '如果只有两级节点
If e.node.checked = False '节点取消选中,父节点随之取消
e.node.ParentNode.Checked = False
End If
If e.node.checked = True '节点选中,遍历同级节点,如果同级节点全部选中,则父节点也选中
Dim Inum As Integer = e.Node.ParentNode.Nodes.Count
For Each nd As WinForm.TreeNode In e.Node.ParentNode.Nodes
If nd.Checked Then
Inum = Inum -1
End If
Next
If Inum = 0 Then
e.Node.ParentNode.Checked = True
End If
End If
End If
If e.node.level =2 Then '如果有3级节点
If e.node.checked = False '如果节点取消选中,则上两级父节点取消选中
e.node.ParentNode.Checked = False
e.node.ParentNode.ParentNode.Checked = False
End If
If e.node.checked = True Then '如果节点选中,遍历同级节点,如果全部选中,则上级节点选中,再遍历上级节点,如果全部选中,则上上级节点选中。4级5级以此类推...
Dim nd1 As WinForm.TreeNode = e.node.ParentNode
Dim nd0 As WinForm.TreeNode = nd1.ParentNode
Dim Inum1 As Integer = nd1.Nodes.Count
Dim Inum2 As Integer = nd0.allNodes.Count
For Each nd As WinForm.TreeNode In nd1.Nodes
If nd.Checked Then
Inum1 = Inum1 -1
End If
Next
If Inum1 = 0 Then
nd1.Checked = True
End If
For Each nd As WinForm.TreeNode In nd0.allNodes
If nd.Checked Then
Inum2 = Inum2 -1
End If
Next
If Inum2 = 0 Then
nd0.Checked = True
End If
End If
End If
If e.node.level = 3 Then ‘4级节点
If e.node.checked = False
e.node.ParentNode.Checked = False
e.node.ParentNode.ParentNode.Checked = False
e.node.ParentNode.ParentNode.ParentNode.Checked = False
End If
If e.node.checked = True Then
Dim nd2 As WinForm.TreeNode = e.node.ParentNode
Dim nd1 As WinForm.TreeNode = nd2.ParentNode
Dim nd0 As WinForm.TreeNode = nd1.ParentNode
Dim Inum1 As Integer = nd2.Nodes.Count
Dim Inum2 As Integer = nd1.allNodes.Count
Dim Inum3 As Integer = nd0.allNodes.Count
For Each nd As WinForm.TreeNode In nd2.Nodes
If nd.Checked Then
Inum1 = Inum1 -1
End If
Next
If Inum1 = 0 Then
nd2.Checked = True
End If
For Each nd As WinForm.TreeNode In nd1.allNodes
If nd.Checked Then
Inum2 = Inum2 -1
End If
Next
If Inum2 = 0 Then
nd1.Checked = True
End If
For Each nd As WinForm.TreeNode In nd0.allNodes
If nd.Checked Then
Inum3 = Inum3 -1
End If
Next
If Inum3 = 0 Then
nd0.Checked = True
End If
End If
End If
If e.node.level = 4 Then ’5级节点
If e.node.checked = False
e.node.ParentNode.Checked = False
e.node.ParentNode.ParentNode.Checked = False
e.node.ParentNode.ParentNode.ParentNode.Checked = False
e.node.ParentNode.ParentNode.ParentNode.ParentNode.Checked = False
End If
If e.node.checked = True Then
Dim nd3 As WinForm.TreeNode = e.node.ParentNode
Dim nd2 As WinForm.TreeNode = nd3.ParentNode
Dim nd1 As WinForm.TreeNode = nd2.ParentNode
Dim nd0 As WinForm.TreeNode = nd1.ParentNode
Dim Inum1 As Integer = nd3.Nodes.Count
Dim Inum2 As Integer = nd2.allNodes.Count
Dim Inum3 As Integer = nd1.allNodes.Count
Dim Inum4 As Integer = nd0.allNodes.Count
For Each nd As WinForm.TreeNode In nd3.Nodes
If nd.Checked Then
Inum1 = Inum1 -1
End If
Next
If Inum1 = 0 Then
nd3.Checked = True
End If
For Each nd As WinForm.TreeNode In nd2.allNodes
If nd.Checked Then
Inum2 = Inum2 -1
End If
Next
If Inum2 = 0 Then
nd2.Checked = True
End If
For Each nd As WinForm.TreeNode In nd1.allNodes
If nd.Checked Then
Inum3 = Inum3 -1
End If
Next
If Inum3 = 0 Then
nd1.Checked = True
End If
For Each nd As WinForm.TreeNode In nd0.allNodes
If nd.Checked Then
Inum4 = Inum4 -1
End If
Next
If Inum4 = 0 Then
nd0.Checked = True
End If
End If
End If
For Each nd As WinForm.TreeNode In trv.AllNodes
If nd.Level > 0 AndAlso nd.ParentNode.Checked Then '如果父节点选中
Continue For '跳过此节点,处理下一结点
End If
If nd.Checked Then
If flt > "" Then
flt = flt & " Or " '注意用or而不是And
End If
If nd.Level = 0 Then '注意下面的条件都要用括号括起来
flt = flt & "( 第一级 = '" & nd.Text & "')"
ElseIf nd.Level = 1 Then
flt = flt & "( 第一级 = '" & nd.ParentNode.Text & "' And 第二级 = '" & nd.Text & "')"
ElseIf nd.Level = 2 Then
flt = flt & "( 第一级 = '" & nd.ParentNode.parentnode.Text & "' and 第二级 = '" & nd.ParentNode.Text & "' And 第三级 = '" & nd.Text & "')"
ElseIf nd.Level = 3 Then
flt = flt & "( 第一级 = '" & nd.ParentNode.ParentNode.ParentNode.Text & "' and 第二级 = '" & nd.ParentNode.ParentNode.Text & "' and 第三级 = '" & nd.ParentNode.Text & "' And 第四级 = '" & nd.Text & "')"
ElseIf nd.Level = 4
flt = flt & "( 第一级 = '" & nd.ParentNode.ParentNode.ParentNode.ParentNode.Text & "' and 第二级 = '" & nd.ParentNode.ParentNode.ParentNode.Text & "' and 第三级 = '" & nd.ParentNode.ParentNode.Text & "' and 第四级 = '" & nd.ParentNode.Text & "' And 第五级 = '" & nd.Text & "')"
End If
End If
Next
Tables("xxx").filter = flt