TreeViewCancelEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 BeforeCheck 控件的 BeforeCollapse、BeforeExpand、BeforeSelect 和 TreeView 事件提供数据。
public ref class TreeViewCancelEventArgs : System::ComponentModel::CancelEventArgs
public class TreeViewCancelEventArgs : System.ComponentModel.CancelEventArgs
type TreeViewCancelEventArgs = class
inherit CancelEventArgs
Public Class TreeViewCancelEventArgs
Inherits CancelEventArgs
- 继承
示例
以下示例演示如何更改 的 TreeView 折叠状态,以便所有选中的节点都可见。 首先,所有节点都折叠,并将一个处理程序添加到 TreeView.BeforeExpand 事件。 接下来,展开所有节点。 事件处理程序 TreeView.BeforeExpand 确定给定节点是否具有检查的子节点。 如果某个节点没有选中的子级,则会取消该节点的扩展。 为了在单击节点旁边的加号时允许正常扩展节点, TreeView.BeforeExpand 然后删除事件处理程序。
也可以通过处理 TreeView.BeforeCollapse 事件来实现此行为,如该主题的示例所示。
有关完整示例,请参阅 TreeView.CheckBoxes 参考主题。
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.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 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
' 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
' 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
注解
有关处理事件的详细信息,请参阅 处理和引发事件。
构造函数
TreeViewCancelEventArgs(TreeNode, Boolean, TreeViewAction) |
用指定的树节点、指定是否取消该事件的值以及引发该事件的树视图操作类型来初始化 TreeViewCancelEventArgs 类的新实例。 |
属性
Action |
获取引发该事件的 TreeView 操作的类型。 |
Cancel |
获取或设置指示是否应取消事件的值。 (继承自 CancelEventArgs) |
Node |
获取要选中、展开、折叠或选择的树节点。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |