TreeNodeCollection.Clear メソッド
アセンブリ: System.Web (system.web.dll 内)



Clear メソッドを使用して、プログラムで TreeNodeCollection を空にする方法の例を次に示します。Nodes プロパティおよび ChildNodes プロパティは、TreeNodeCollection オブジェクトを返します。
<%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then ' Use the Add and Remove methods to programmatically ' remove the Appendix C node and replace it with a new ' node. LinksTreeView.Nodes.Remove(LinksTreeView.Nodes(3)) LinksTreeView.Nodes.Add(New TreeNode("New Appendix C")) ' Use the AddAt and RemoveAt methods to programmatically ' remove the Chapter One node and replace it with a new node. LinksTreeView.Nodes(0).ChildNodes.RemoveAt(0) LinksTreeView.Nodes(0).ChildNodes.AddAt(0, New TreeNode("New Chapter One")) ' Use the Clear method to remove all the child nodes of the ' Chapter Two node. LinksTreeView.Nodes(0).ChildNodes(1).ChildNodes.Clear() End If End Sub </script> <html> <body> <form runat="server"> <h3>TreeNodeCollection Example</h3> <asp:TreeView id="LinksTreeView" Font-Name= "Arial" ForeColor="Blue" runat="server"> <LevelStyles> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="12pt" ForeColor="DarkGreen"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-UnderLine="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt"/> </LevelStyles> <Nodes> <asp:TreeNode Text="Table of Contents" Expanded="true"> <asp:TreeNode Text="Chapter One"> <asp:TreeNode Text="Section 1.0"> <asp:TreeNode Text="Topic 1.0.1"/> <asp:TreeNode Text="Topic 1.0.2"/> <asp:TreeNode Text="Topic 1.0.3"/> </asp:TreeNode> <asp:TreeNode Text="Section 1.1"> <asp:TreeNode Text="Topic 1.1.1"/> <asp:TreeNode Text="Topic 1.1.2"/> <asp:TreeNode Text="Topic 1.1.3"/> <asp:TreeNode Text="Topic 1.1.4"/> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Chapter Two"> <asp:TreeNode Text="Section 2.0"> <asp:TreeNode Text="Topic 2.0.1"/> <asp:TreeNode Text="Topic 2.0.2"/> </asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Appendix A" /> <asp:TreeNode Text="Appendix B" /> <asp:TreeNode Text="Appendix C" /> </Nodes> </asp:TreeView> </form> </body> </html>
<%@ Page Language="C#" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { if (!IsPostBack) { // Use the Add and Remove methods to programmatically // remove the Appendix C node and replace it with a new // node. LinksTreeView.Nodes.Remove(LinksTreeView.Nodes[3]); LinksTreeView.Nodes.Add(new TreeNode("New Appendix C")); // Use the AddAt and RemoveAt methods to programmatically // remove the Chapter One node and replace it with a new node. LinksTreeView.Nodes[0].ChildNodes.RemoveAt(0); LinksTreeView.Nodes[0].ChildNodes.AddAt(0, new TreeNode("New Chapter One")); // Use the Clear method to remove all the child nodes of the // Chapter Two node. LinksTreeView.Nodes[0].ChildNodes[1].ChildNodes.Clear(); } } </script> <html> <body> <form runat="server"> <h3>TreeNodeCollection Example</h3> <asp:TreeView id="LinksTreeView" Font-Name= "Arial" ForeColor="Blue" runat="server"> <LevelStyles> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="12pt" ForeColor="DarkGreen"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="5" Font-UnderLine="true" Font-Size="10pt"/> <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt"/> </LevelStyles> <Nodes> <asp:TreeNode Text="Table of Contents" Expanded="true"> <asp:TreeNode Text="Chapter One"> <asp:TreeNode Text="Section 1.0"> <asp:TreeNode Text="Topic 1.0.1"/> <asp:TreeNode Text="Topic 1.0.2"/> <asp:TreeNode Text="Topic 1.0.3"/> </asp:TreeNode> <asp:TreeNode Text="Section 1.1"> <asp:TreeNode Text="Topic 1.1.1"/> <asp:TreeNode Text="Topic 1.1.2"/> <asp:TreeNode Text="Topic 1.1.3"/> <asp:TreeNode Text="Topic 1.1.4"/> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Chapter Two"> <asp:TreeNode Text="Section 2.0"> <asp:TreeNode Text="Topic 2.0.1"/> <asp:TreeNode Text="Topic 2.0.2"/> </asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Appendix A" /> <asp:TreeNode Text="Appendix B" /> <asp:TreeNode Text="Appendix C" /> </Nodes> </asp:TreeView> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TreeNodeCollection.Clear メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


このメソッドを使用すると、ツリー ビューからツリー ノードのコレクション全体を削除できます。
コレクションからツリー ノードを個別に削除するには、Remove メソッドまたは RemoveAt メソッドを使用します。
新しい TreeNode オブジェクトをコレクションに追加するには、Add メソッド、AddRange メソッド、または Insert メソッドを使用します。

TreeNodeCollection を TreeView から一時的な Array にコピーし、この配列の内容を AddRange メソッドを使用して別の TreeView に追加するコード例を次に示します。元の TreeView の TreeNodeCollection は、Clear メソッドによって削除されます。この例は、TreeView コントロールが 2 つあり、一方には TreeNode オブジェクトのコレクションが設定されていることを前提にしています。
Private Sub MyButtonAddAllClick(sender As Object, e As EventArgs) ' Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView. Dim myTreeNodeCollection As TreeNodeCollection = myTreeViewBase.Nodes ' Create an array of 'TreeNodes'. Dim myTreeNodeArray(myTreeViewBase.Nodes.Count-1) As TreeNode ' Copy the tree nodes to the 'myTreeNodeArray' array. myTreeViewBase.Nodes.CopyTo(myTreeNodeArray, 0) ' Remove all the tree nodes from the 'myTreeViewBase' TreeView. myTreeViewBase.Nodes.Clear() ' Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView. myTreeViewCustom.Nodes.AddRange(myTreeNodeArray) End Sub
private void MyButtonAddAllClick(object sender, EventArgs e) { // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView. TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes; // Create an array of 'TreeNodes'. TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count]; // Copy the tree nodes to the 'myTreeNodeArray' array. myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0); // Remove all the tree nodes from the 'myTreeViewBase' TreeView. myTreeViewBase.Nodes.Clear(); // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView. myTreeViewCustom.Nodes.AddRange(myTreeNodeArray); }
private: void MyButtonAddAllClick( Object^ /*sender*/, EventArgs^ /*e*/ ) { // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView. TreeNodeCollection^ myTreeNodeCollection = myTreeViewBase->Nodes; // Create an array of 'TreeNodes'. array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myTreeViewBase->Nodes->Count); // Copy the tree nodes to the 'myTreeNodeArray' array. myTreeViewBase->Nodes->CopyTo( myTreeNodeArray, 0 ); // Remove all the tree nodes from the 'myTreeViewBase' TreeView. myTreeViewBase->Nodes->Clear(); // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView. myTreeViewCustom->Nodes->AddRange( myTreeNodeArray ); }
private void myButtonAddAllClick(Object sender, EventArgs e) { // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView. TreeNodeCollection myTreeNodeCollection = myTreeViewBase.get_Nodes(); // Create an array of 'TreeNodes'. TreeNode myTreeNodeArray[] = new TreeNode[myTreeViewBase.get_Nodes().get_Count()]; // Copy the tree nodes to the 'myTreeNodeArray' array. myTreeViewBase.get_Nodes().CopyTo(myTreeNodeArray, 0); // Remove all the tree nodes from the 'myTreeViewBase' TreeView. myTreeViewBase.get_Nodes().Clear(); // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView. myTreeViewCustom.get_Nodes().AddRange(myTreeNodeArray); } //myButtonAddAllClick

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に収録されているすべての辞書からTreeNodeCollection.Clearを検索する場合は、下記のリンクをクリックしてください。

- TreeNodeCollection.Clearのページへのリンク