共享:如何高效使用Treeview中的Checkbox
'向下递归 Private Sub foreward(ByVal parent As TreeNode) For Each child As TreeNode In parent.Nodes If child IsNot Nothing Then child.Checked = parent.Checked foreward(child) End If Next End Sub '向上递归 Private Sub backward(ByVal child As TreeNode) Select Case child.Level Case Is > 0 Dim b As Boolean = True For Each Node As TreeNode In child.Parent.Nodes If Node IsNot Nothing Then b = b And Node.Checked Next child.Parent.Checked = b backward(child.Parent) End Select End Sub '点击后此过程只许运行一次! Private only As Boolean = True Private Sub TreeView_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck If Not only Then Return only = False foreward(e.Node) backward(e.Node) only = True End Sub