TreeViewCancelEventArgs 类
为 TreeView 控件的 BeforeCheck、BeforeCollapse、BeforeExpand 和 BeforeSelect 事件提供数据。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Class TreeViewCancelEventArgs
Inherits CancelEventArgs
用法
Dim instance As TreeViewCancelEventArgs
public class TreeViewCancelEventArgs : CancelEventArgs
public ref class TreeViewCancelEventArgs : public CancelEventArgs
public class TreeViewCancelEventArgs extends CancelEventArgs
public class TreeViewCancelEventArgs extends CancelEventArgs
备注
有关处理事件的更多信息,请参见 使用事件。
示例
下面的示例阐释了如何更改 TreeView 的折叠状态,以便所有选中的节点都可见。首先,折叠所有节点,并将一个处理程序添加到 TreeView.BeforeExpand 事件中。然后,展开所有节点。TreeView.BeforeExpand 事件处理程序确定给定节点是否有选中的子节点。如果某个节点没有选中的子节点,则取消该节点的展开操作。为了在单击节点旁边的加号时允许正常的节点展开操作,TreeView.BeforeExpand 事件处理程序于是被删除。
此行为还可以通过处理 TreeView.BeforeCollapse 事件实现,该主题的示例中阐释了这一点。
有关完整的示例,请参见 TreeView.CheckBoxes 参考主题。
Private Sub showCheckedNodesButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Disable redrawing of treeView1 to prevent flickering
' while changes are made.
treeView1.BeginUpdate()
' Collapse all nodes of treeView1.
treeView1.CollapseAll()
' Add the CheckForCheckedChildren event handler to the BeforeExpand event.
AddHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren
' Expand all nodes of treeView1. Nodes without checked children are
' prevented from expanding by the checkForCheckedChildren event handler.
treeView1.ExpandAll()
' Remove the checkForCheckedChildren event handler from the BeforeExpand
' event so manual node expansion will work correctly.
RemoveHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren
' Enable redrawing of treeView1.
treeView1.EndUpdate()
End Sub 'showCheckedNodesButton_Click
' Prevent expansion of a node that does not have any checked child nodes.
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
If Not HasCheckedChildNodes(e.Node) Then
e.Cancel = True
End If
End Sub 'CheckForCheckedChildren
' Returns a value indicating whether the specified
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean
If node.Nodes.Count = 0 Then
Return False
End If
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Checked Then
Return True
End If
' Recursively check the children of the current child node.
If HasCheckedChildNodes(childNode) Then
Return True
End If
Next childNode
Return False
End Function 'HasCheckedChildNodes
private void showCheckedNodesButton_Click(object sender, EventArgs e)
{
// Disable redrawing of treeView1 to prevent flickering
// while changes are made.
treeView1.BeginUpdate();
// Collapse all nodes of treeView1.
treeView1.CollapseAll();
// Add the checkForCheckedChildren event handler to the BeforeExpand event.
treeView1.BeforeExpand += checkForCheckedChildren;
// Expand all nodes of treeView1. Nodes without checked children are
// prevented from expanding by the checkForCheckedChildren event handler.
treeView1.ExpandAll();
// Remove the checkForCheckedChildren event handler from the BeforeExpand
// event so manual node expansion will work correctly.
treeView1.BeforeExpand -= checkForCheckedChildren;
// Enable redrawing of treeView1.
treeView1.EndUpdate();
}
// Prevent expansion of a node that does not have any checked child nodes.
private void CheckForCheckedChildrenHandler(object sender,
TreeViewCancelEventArgs e)
{
if (!HasCheckedChildNodes(e.Node)) e.Cancel = true;
}
// Returns a value indicating whether the specified
// TreeNode has checked child nodes.
private bool HasCheckedChildNodes(TreeNode node)
{
if (node.Nodes.Count == 0) return false;
foreach (TreeNode childNode in node.Nodes)
{
if (childNode.Checked) return true;
// Recursively check the children of the current child node.
if (HasCheckedChildNodes(childNode)) return true;
}
return false;
}
private:
void showCheckedNodesButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
// Disable redrawing of treeView1 to prevent flickering
// while changes are made.
treeView1->BeginUpdate();
// Collapse all nodes of treeView1.
treeView1->CollapseAll();
// Add the checkForCheckedChildren event handler to the BeforeExpand event.
treeView1->BeforeExpand += checkForCheckedChildren;
// Expand all nodes of treeView1. Nodes without checked children are
// prevented from expanding by the checkForCheckedChildren event handler.
treeView1->ExpandAll();
// Remove the checkForCheckedChildren event handler from the BeforeExpand
// event so manual node expansion will work correctly.
treeView1->BeforeExpand -= checkForCheckedChildren;
// Enable redrawing of treeView1.
treeView1->EndUpdate();
}
// Prevent expansion of a node that does not have any checked child nodes.
void CheckForCheckedChildrenHandler( Object^ /*sender*/, TreeViewCancelEventArgs^ e )
{
if ( !HasCheckedChildNodes( e->Node ) )
e->Cancel = true;
}
// Returns a value indicating whether the specified
// TreeNode has checked child nodes.
bool HasCheckedChildNodes( TreeNode^ node )
{
if ( node->Nodes->Count == 0 )
return false;
System::Collections::IEnumerator^ myEnum = node->Nodes->GetEnumerator();
while ( myEnum->MoveNext() )
{
TreeNode^ childNode = safe_cast<TreeNode^>(myEnum->Current);
if ( childNode->Checked )
return true;
// Recursively check the children of the current child node.
if ( HasCheckedChildNodes( childNode ) )
return true;
}
return false;
}
private void showCheckedNodesButton_Click(Object sender, EventArgs e)
{
// Disable redrawing of treeView1 to prevent flickering
// while changes are made.
treeView1.BeginUpdate();
// Collapse all nodes of treeView1.
treeView1.CollapseAll();
// Add the checkForCheckedChildren event handler to the
// BeforeExpand event.
treeView1.add_BeforeExpand(checkForCheckedChildren);
// Expand all nodes of treeView1. Nodes without checked children are
// prevented from expanding by the checkForCheckedChildren event handler.
treeView1.ExpandAll();
// Remove the checkForCheckedChildren event handler from
// the BeforeExpand
// event so manual node expansion will work correctly.
treeView1.remove_BeforeExpand(checkForCheckedChildren);
// Enable redrawing of treeView1.
treeView1.EndUpdate();
} //showCheckedNodesButton_Click
// Prevent expansion of a node that does not have any checked child nodes.
private void CheckForCheckedChildrenHandler(Object sender,
TreeViewCancelEventArgs e)
{
if (!(HasCheckedChildNodes(e.get_Node()))) {
e.set_Cancel(true);
}
} //CheckForCheckedChildrenHandler
// Returns a value indicating whether the specified
// TreeNode has checked child nodes.
private boolean HasCheckedChildNodes(TreeNode node)
{
if (node.get_Nodes().get_Count() == 0) {
return false;
}
for (int iCtr = 0; iCtr < node.get_Nodes().get_Count(); iCtr++) {
TreeNode childNode = node.get_Nodes().get_Item(iCtr);
if (childNode.get_Checked()) {
return true;
}
// Recursively check the children of the current child node.
if (HasCheckedChildNodes(childNode)) {
return true;
}
}
return false;
} //HasCheckedChildNodes
继承层次结构
System.Object
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Windows.Forms.TreeViewCancelEventArgs
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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、1.0
请参见
参考
TreeViewCancelEventArgs 成员
System.Windows.Forms 命名空间
TreeViewEventArgs