TreeView.AfterCollapse 事件
在折叠树节点后发生。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Event AfterCollapse As TreeViewEventHandler
用法
Dim instance As TreeView
Dim handler As TreeViewEventHandler
AddHandler instance.AfterCollapse, handler
public event TreeViewEventHandler AfterCollapse
public:
event TreeViewEventHandler^ AfterCollapse {
void add (TreeViewEventHandler^ value);
void remove (TreeViewEventHandler^ value);
}
/** @event */
public void add_AfterCollapse (TreeViewEventHandler value)
/** @event */
public void remove_AfterCollapse (TreeViewEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
有关处理事件的更多信息,请参见 使用事件。
示例
当用户更改 TreeNode 的选中状态时,下面的代码示例将更新它的所有子树节点。这段代码要求有一个包含 TreeView 的 Form,该树视图在它的 TreeNodeCollection 中包含有 TreeNode 对象。TreeNodeCollection 应该含有包含子节点的树节点。
' 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
// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
}
// 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 void node_AfterCheck(object sender, TreeViewEventArgs e)
{
// The code only executes if the user caused the checked state to change.
if(e.Action != TreeViewAction.Unknown)
{
if(e.Node.Nodes.Count > 0)
{
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}
// Updates all child tree nodes recursively.
void CheckAllChildNodes( TreeNode^ treeNode, bool nodeChecked )
{
IEnumerator^ myEnum = treeNode->Nodes->GetEnumerator();
while ( myEnum->MoveNext() )
{
TreeNode^ node = safe_cast<TreeNode^>(myEnum->Current);
node->Checked = nodeChecked;
if ( node->Nodes->Count > 0 )
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this->CheckAllChildNodes( node, nodeChecked );
}
}
}
// 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.
void node_AfterCheck( Object^ /*sender*/, TreeViewEventArgs^ e )
{
// The code only executes if the user caused the checked state to change.
if ( e->Action != TreeViewAction::Unknown )
{
if ( e->Node->Nodes->Count > 0 )
{
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this->CheckAllChildNodes( e->Node, e->Node->Checked );
}
}
}
// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, boolean nodeChecked)
{
for (int iCtr = 0; iCtr < treeNode.get_Nodes().get_Count(); iCtr++) {
TreeNode node = treeNode.get_Nodes().get_Item(iCtr);
node.set_Checked(nodeChecked);
if (node.get_Nodes().get_Count() > 0) {
// If the current node has child nodes, call the
// CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
} //CheckAllChildNodes
// 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 void Node_AfterCheck(Object sender, TreeViewEventArgs e)
{
// The code only executes if the user caused the checked state to change.
if (!(e.get_Action().Equals(TreeViewAction.Unknown))) {
if (e.get_Node().get_Nodes().get_Count() > 0) {
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this.CheckAllChildNodes(e.get_Node(), e.get_Node().get_Checked());
}
}
} //Node_AfterCheck
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0
请参见
参考
TreeView 类
TreeView 成员
System.Windows.Forms 命名空间
OnAfterCollapse
BeforeCollapse
OnBeforeCollapse