我对这个树视业务都很陌生。我有一个固定的树视图,非常简单和基本。只有两个级别,父节点和子节点。我只需要从树视图中得到两件事。
1)如果选中parent,请检查所有子节点。
2)如果未选中一个子节点,请取消选中父节点。
就是这样!我一直在寻找这个30分钟,我找不到它。也许是因为我的vb.net很旧,它似乎与其他人不一样= /我正在使用vb.net 2003.非常感谢你:D
答案 0 :(得分:3)
VB.Net 2008中的工作示例,但 应该在2003年工作:
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")
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
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
答案 1 :(得分:1)
您可能想尝试这个较短的版本
Private Sub treeview1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeview1.AfterCheck
Dim objNode As TreeNode = e.Node
Dim bCheck As Boolean = e.Node.Checked
For Each ChildNode As TreeNode In objNode.Nodes
ChildNode.Checked = bCheck
Next
End Sub
答案 2 :(得分:1)
我的要求基本相同,但我也想要检查所有子节点,检查父节点。
这是我今天在VS 2013中测试的代码:
$(document).ready(function() {
var table = $('#example').DataTable( {
"responsive": true,
colReorder: {
order: [5,4,3,0,1,2]
}
} );
} );
答案 3 :(得分:0)
这适用于VB.net 2010.它基于Daniel Lee代码
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
答案 4 :(得分:0)
Private Sub Tv1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles Tv1.AfterCheck
Try
Dim A As Boolean = e.Node.Checked
If e.Action = TreeViewAction.ByMouse Or e.Action = TreeViewAction.ByKeyboard Then
CUchkall(e.Node, A)
End If
UnCheckParentNodes(e.Node)
Call CuChild(e.Node)
Catch
End Try
End Sub
Private Sub CuChild(ByVal iNode As TreeNode)
Try
Dim E As Boolean = True
If iNode.Parent IsNot Nothing Then
For Each M As TreeNode In iNode.Parent.Nodes
If M.Checked = False Then E = False
Next
iNode.Parent.Checked = E
End If
Catch
End Try
End Sub
Private Sub CUchkall(ByVal iNode As TreeNode, COU As Boolean)
Try
If iNode.Nodes IsNot Nothing Then
For Each N As TreeNode In iNode.Nodes
N.Checked = COU
Call CUchkall(N, COU)
Next
End If
Catch
End Try
End Sub
Private Sub UnCheckParentNodes(ByVal iNode As TreeNode)
Try
If iNode.Checked = False AndAlso iNode.Parent IsNot Nothing Then
iNode.Parent.Checked = False
UnCheckParentNodes(iNode.Parent)
End If
Catch
End Try
End Sub
答案 5 :(得分:0)
我为多层树视图(递归版本)修改了Javier Arce代码。这是我的代码。
Private Sub treeTestsSelection_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeTestsSelection.AfterCheck
If e.Action <> TreeViewAction.ByMouse And e.Action <> TreeViewAction.ByKeyboard Then Exit Sub
CheckAllNodes(e.Node)
CheckAllNodesForParent(e.Node)
End Sub
Public Sub CheckAllNodes(ByRef TreeNodeToCheck As TreeNode)
Dim val As Boolean = TreeNodeToCheck.Checked
For Each n As TreeNode In TreeNodeToCheck.Nodes
n.Checked = val
If n.GetNodeCount(True) > 0 Then
CheckAllNodes(n)
End If
Next
End Sub
Public Sub CheckAllNodesForParent(ByRef CheckedTreeNode As TreeNode)
If CheckedTreeNode.Checked Then
If CheckedTreeNode.Parent Is Nothing = False Then
Dim allChecked As Boolean = True
For Each node As TreeNode In CheckedTreeNode.Parent.Nodes
If Not node.Checked Then
allChecked = False
End If
Next
If allChecked Then
CheckedTreeNode.Parent.Checked = True
CheckAllNodesForParent(CheckedTreeNode.Parent)
End If
End If
Else
If CheckedTreeNode.Parent Is Nothing = False Then
CheckedTreeNode.Parent.Checked = False
CheckAllNodesForParent(CheckedTreeNode.Parent)
End If
End If
End Sub