TreeView.BeforeCollapse イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As TreeView Dim handler As TreeViewCancelEventHandler AddHandler instance.BeforeCollapse, handler
public: event TreeViewCancelEventHandler^ BeforeCollapse { void add (TreeViewCancelEventHandler^ value); void remove (TreeViewCancelEventHandler^ value); }


TreeView の折りたたみの状態を変更して、チェックされているノードを表示する方法を次のコード例に示します。まず、すべてのノードが展開され、BeforeCollapse イベントのイベント ハンドラが追加されます。次に、すべてのノードが折りたたまれます。BeforeCollapse イベント ハンドラは、指定したノードに、チェックされている子ノードがあるかどうかを確認します。チェックされている子ノードがある場合、そのノードの折りたたみはキャンセルされます。次に、ノードの横にあるマイナス記号がクリックされたときに通常のノードを折りたためるようにするため、BeforeCollapse イベント ハンドラが削除されます。
BeforeExpand イベントを処理してこの動作を実装することもできます。詳細については、該当するトピックの例を参照してください。
詳細については、TreeView コンストラクタのリファレンス トピックを参照してください。
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.ExpandAll() ' Add the CheckForCheckedChildren event handler to the BeforeExpand event. AddHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren ' Expand all nodes of treeView1. Nodes without checked children are ' prevented from expanding by the checkForCheckedChildren event handler. treeView1.CollapseAll() ' Remove the checkForCheckedChildren event handler from the BeforeExpand ' event so manual node expansion will work correctly. RemoveHandler treeView1.BeforeCollapse, AddressOf CheckForCheckedChildren ' Enable redrawing of treeView1. treeView1.EndUpdate() End Sub 'showCheckedNodesButton_Click ' Prevent collapse of a node that has checked child nodes. Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) If 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.ExpandAll(); // Add the checkForCheckedChildren event handler to the BeforeExpand event. treeView1.BeforeCollapse += checkForCheckedChildren; // Expand all nodes of treeView1. Nodes without checked children are // prevented from expanding by the checkForCheckedChildren event handler. treeView1.CollapseAll(); // Remove the checkForCheckedChildren event handler from the BeforeExpand // event so manual node expansion will work correctly. treeView1.BeforeCollapse -= checkForCheckedChildren; // Enable redrawing of treeView1. treeView1.EndUpdate(); } // Prevent collapse of a node that has 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->ExpandAll(); // Add the checkForCheckedChildren event handler to the BeforeExpand event. treeView1->BeforeCollapse += checkForCheckedChildren; // Expand all nodes of treeView1. Nodes without checked children are // prevented from expanding by the checkForCheckedChildren event handler. treeView1->CollapseAll(); // Remove the checkForCheckedChildren event handler from the BeforeExpand // event so manual node expansion will work correctly. treeView1->BeforeCollapse -= checkForCheckedChildren; // Enable redrawing of treeView1. treeView1->EndUpdate(); } // Prevent collapse of a node that has 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.ExpandAll(); // Add the checkForCheckedChildren event handler to the // BeforeExpand event. treeView1.add_BeforeCollapse(checkForCheckedChildren); // Expand all nodes of treeView1. Nodes without checked children are // prevented from expanding by the checkForCheckedChildren event handler. treeView1.CollapseAll(); // Remove the checkForCheckedChildren event handler from the // BeforeExpand event so manual node expansion will work correctly. treeView1.remove_BeforeCollapse(checkForCheckedChildren); // Enable redrawing of treeView1. treeView1.EndUpdate(); } //showCheckedNodesButton_Click // Prevent collapse of a node that has 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTreeView.BeforeCollapse イベントを検索する場合は、下記のリンクをクリックしてください。

- TreeView.BeforeCollapse イベントのページへのリンク