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
스레드로부터의 안전성
이 형식의 모든 public static(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