TreeViewCancelEventArgs クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)



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.EventArgs
System.ComponentModel.CancelEventArgs
System.Windows.Forms.TreeViewCancelEventArgs


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TreeViewCancelEventArgs コンストラクタ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim node As TreeNode Dim cancel As Boolean Dim action As TreeViewAction Dim instance As New TreeViewCancelEventArgs(node, cancel, action)
public function TreeViewCancelEventArgs ( node : TreeNode, cancel : boolean, action : TreeViewAction )

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TreeViewCancelEventArgs プロパティ

名前 | 説明 | |
---|---|---|
![]() | Cancel | イベントをキャンセルするかどうかを示す値を取得または設定します。 ( CancelEventArgs から継承されます。) |
![]() | Node | チェックされる、展開される、折りたたまれる、または選択されるツリー ノードを取得します。 |

TreeViewCancelEventArgs メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

TreeViewCancelEventArgs メンバ
TreeView コントロールの BeforeCheck、BeforeCollapse、BeforeExpand、および BeforeSelect の各イベントのデータを提供します。
TreeViewCancelEventArgs データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | TreeViewCancelEventArgs | ツリー ノード、イベントをキャンセルするかどうかを示す値、およびイベントを発生させたツリー ビュー アクションの種類を指定して、TreeViewCancelEventArgs クラスの新しいインスタンスを初期化します。 |

名前 | 説明 | |
---|---|---|
![]() | Cancel | イベントをキャンセルするかどうかを示す値を取得または設定します。(CancelEventArgs から継承されます。) |
![]() | Node | チェックされる、展開される、折りたたまれる、または選択されるツリー ノードを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

- TreeViewCancelEventArgsのページへのリンク