TreeView.BeforeExpand 이벤트
트리 노드가 확장되기 전에 발생합니다.
네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)
구문
‘선언
Public Event BeforeExpand As TreeViewCancelEventHandler
‘사용 방법
Dim instance As TreeView
Dim handler As TreeViewCancelEventHandler
AddHandler instance.BeforeExpand, handler
public event TreeViewCancelEventHandler BeforeExpand
public:
event TreeViewCancelEventHandler^ BeforeExpand {
void add (TreeViewCancelEventHandler^ value);
void remove (TreeViewCancelEventHandler^ value);
}
/** @event */
public void add_BeforeExpand (TreeViewCancelEventHandler value)
/** @event */
public void remove_BeforeExpand (TreeViewCancelEventHandler value)
JScript에서는 이벤트를 사용할 수 있지만 새로 선언할 수는 없습니다.
설명
이벤트 처리에 대한 자세한 내용은 이벤트 사용을 참조하십시오.
예제
다음 코드 예제에서는 선택된 노드가 모두 표시되도록 TreeView의 축소 상태를 변경하는 방법을 보여 줍니다. 먼저 모든 노드가 축소되고 BeforeExpand 이벤트에 대한 처리기가 추가됩니다. 다음에는 모든 노드가 확장됩니다. BeforeExpand 이벤트 처리기는 특정 노드에 선택된 자식 노드가 있는지 여부를 확인합니다. 노드에 선택된 자식 노드가 없으면 해당 노드에 대한 확장이 취소됩니다. 그런 다음 노드 옆의 더하기 기호를 클릭할 때 표준 노드 확장 작업을 허용하기 위해 BeforeExpand 이벤트 처리기가 제거됩니다.
이 동작은 해당 항목에 대한 예제에서 보여 준 것처럼 BeforeCollapse 이벤트를 처리하여 구현할 수도 있습니다.
자세한 예제를 보려면 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
플랫폼
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에서 지원
참고 항목
참조
TreeView 클래스
TreeView 멤버
System.Windows.Forms 네임스페이스
OnBeforeExpand
TreeView.AfterExpand 이벤트
OnAfterExpand