TreeView コントロールの BeforeCheck 、 BeforeCollapse 、 BeforeExpand 、 BeforeSelect の各イベントのデータを提供します。
この型のすべてのメンバの一覧については、TreeViewCancelEventArgs メンバ を参照してください。
System.Object
System.EventArgs
System.ComponentModel.CancelEventArgs
System.Windows.Forms.TreeViewCancelEventArgs
Public Class TreeViewCancelEventArgs
Inherits CancelEventArgs
[C#]
public class TreeViewCancelEventArgs : CancelEventArgs
[C++]
public __gc class TreeViewCancelEventArgs : public CancelEventArgs
[JScript]
public class TreeViewCancelEventArgs extends CancelEventArgs
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
イベント処理の詳細については、「 イベントの利用 」を参照してください。
使用例
[Visual Basic, C#, C++] TreeView の折りたたみの状態を変更して、チェックされているノードを表示する方法を次の例に示します。まず、すべてのノードが折りたたまれ、 TreeView.BeforeExpand イベントにハンドラが追加されます。次に、すべてのノードが展開されます。 TreeView.BeforeExpand イベント ハンドラは、指定したノードに、チェックされている子ノードがあるかどうかを確認します。チェックされている子ノードがない場合、そのノードの展開はキャンセルされます。次に、ノードの横にあるプラス記号がクリックされたときに通常のノードを展開できるようにするため、 TreeView.BeforeExpand イベント ハンドラが削除されます。
[Visual Basic, C#, C++] TreeView.BeforeCollapse イベントを処理してこの動作を実装することもできます。詳細については、該当するトピックの例を参照してください。
[Visual Basic, C#, C++] 詳細については、 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
[C#]
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;
}
[C++]
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 = __try_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;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Windows.Forms
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)
参照
TreeViewCancelEventArgs メンバ | System.Windows.Forms 名前空間 | TreeViewEventArgs