Private Sub tvLevel_NodeClick(ByVal Node As MSComctlLib.Node) '/*如果当前为未选中或模糊状态,则变为选中,否则为变为未选中 '/*如果变为选中状态,则所有子节点变为选中 '/*如果变为未选中状态,则所有子节点变为未选中 '/*父节点根据兄弟节点而定 If Node.Image = "selall" Then Node.Image = "selno" Call chgChild(Node, False) Else Node.Image = "selall" Call chgChild(Node, True) End If Call chgParent(Node) End Sub Private Sub chgChild(mNode As MSComctlLib.Node, selected As Boolean) '/*改变所有子节点状态 Dim nodX As Node Dim nodTmp As Node If mNode.Child Is Nothing Then Exit Sub Set nodX = mNode.Child Do While Not nodX.Key = mNode.Key If selected Then nodX.Image = "selall" Else nodX.Image = "selno" End If If Not nodX.Child Is Nothing Then '/*有子节点,指向子节点 Set nodX = nodX.Child Else '/*如果没有子节点,则指向下一个兄弟节点 If Not nodX.Next Is Nothing Then '/*如果有下一个兄弟节点,则指向下一个兄弟节点 Set nodX = nodX.Next Else '/*兄弟已尽,指向父节点,然后指向下一个兄弟节点 Do While (nodX.Next Is Nothing And nodX.Key <> mNode.Key) Set nodX = nodX.Parent Loop End If End If Loop End Sub Private Sub chgParent(mNode As MSComctlLib.Node) '/*改变父级节点的状态 Dim count As Integer '/*标志 Dim nodX As Node Dim nodTmp As Node Dim i As Integer Set nodX = mNode.Parent Do While Not nodX Is Nothing '/*判断一个节点状态 count = 0 Set nodTmp = nodX.Child For i = 1 To nodX.Children If nodTmp.Image = "selall" Then count = count + 2 End If If nodTmp.Image = "selsome" Then count = count + 1 End If If i < nodX.Children Then Set nodTmp = nodTmp.Next End If Next i nodX.Image = "selsome" If count = 2 * nodX.Children Then '/*全为选中状态 nodX.Image = "selall" End If If count = 0 Then '/*全为未选中状态 nodX.Image = "selno" End If Set nodX = nodX.Parent Loop End Sub treeview不使用checkbox,因为它只有两种状态 如果您有更好的方法,请写在下面:) |
|
来自: 百眼通 > 《03VB和VB.NET-113》