【发布时间】:2015-09-04 15:28:41
【问题描述】:
我正在尝试为 TreeNode AfterCheck 事件创建事件处理程序。我想在检查父母时检查所有孩子。唯一的问题是我不确定在运行时创建 TreeView 时如何执行此操作。此代码假定表单上已有一个名为 treeView1 的 TreeView。我需要做什么才能将 treeView1 替换为尚不存在的 TreeView?
' Updates all child tree nodes recursively.
Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
Dim node As TreeNode
For Each node In treeNode.Nodes
node.Checked = nodeChecked
If node.Nodes.Count > 0 Then
' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
Me.CheckAllChildNodes(node, nodeChecked)
End If
Next node
End Sub
' NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
' After a tree node's Checked property is changed, all its child nodes are updated to the same value.
Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck
' The code only executes if the user caused the checked state to change.
If e.Action <> TreeViewAction.Unknown Then
If e.Node.Nodes.Count > 0 Then
' Calls the CheckAllChildNodes method, passing in the current
' Checked value of the TreeNode whose checked state changed.
Me.CheckAllChildNodes(e.Node, e.Node.Checked)
End If
End If
End Sub
正确的代码
在我添加树视图的地方,我添加了
AddHandler newTree.AfterCheck, AddressOf node_AfterCheck
然后我对上面的代码所做的就是删除它所说的地方
Handles treeView1.AfterCheck
在事件处理程序声明中。
我还能够添加另一个事件处理程序以将光标更改为 Cursors。如果您将鼠标悬停在子节点上,则为否;如果您将鼠标悬停在父节点上,则为手。这是在与后检查处理程序相同的位置添加的。
AddHandler newTree.NodeMouseHover, AddressOf node_MouseOver
这是实际的事件处理程序
' Event handler for node mouse over
Private Sub node_MouseOver(sender As Object, e As TreeNodeMouseHoverEventArgs)
If e.Node.Tag > 99999999 Then
sender.Cursor = Cursors.Hand
Else
sender.Cursor = Cursors.No
End If
End Sub
【问题讨论】:
标签: vb.net