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


Remove メソッドを使用して、指定したノードをコレクションから削除します。削除されるノードの後に続く項目はすべて、空いた位置を埋め合わせるように上に移動します。移動した項目のインデックスも更新されます。
![]() |
---|
TreeNodeCollection には、null 参照 (Visual Basic の場合は Nothing) だけでなく、複製した TreeNode オブジェクトも格納できます。 |
または、RemoveAt メソッドを使用して、特定のインデックス位置にあるノードを削除することもできます。Clear メソッドを使用して、コレクション内のすべてのノードを削除することもできます。

Remove メソッドを使用して、プログラムで 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.Remove メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


ツリー ノード コレクションから TreeNode を削除すると、後続のすべてのツリー ノードの位置が、それぞれコレクション内で 1 つ前に移動します。
追加した TreeNode を削除する別の手段として、RemoveAt メソッドまたは Clear メソッドも使用できます。
![]() |
---|
新しい TreeNode オブジェクトをコレクションに追加するには、Add メソッド、AddRange メソッド、または Insert メソッドを使用します。

一方の TreeView から、選択されているツリー ノードを削除し、これを別のツリー ノード コレクションに追加するコード例を次に示します。ただし、両方のツリー ノード コレクションが読み取り専用ではない必要があります。Button がクリックされると、TreeView.SelectedNode プロパティが示す TreeNode が Remove メソッドを使用して 1 つの TreeView から削除され、Insert メソッドを使用して別の TreeView に追加されます。この例は、Form に、2 つの TreeView コントロールおよび 1 つの Button が配置されていることを前提としています。TreeView コントロールの名前は treeView1 および treeView2 とします。
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click ' If neither TreeNodeCollection is read-only, move the ' selected node from treeView1 to treeView2. If Not treeView1.Nodes.IsReadOnly And Not treeView2.Nodes.IsReadOnly Then If Not (treeView1.SelectedNode Is Nothing) Then Dim tn As TreeNode = treeView1.SelectedNode treeView1.Nodes.Remove(tn) treeView2.Nodes.Insert(treeView2.Nodes.Count, tn) End If End If End Sub
private void button1_Click(object sender, EventArgs e) { // If neither TreeNodeCollection is read-only, move the // selected node from treeView1 to treeView2. if(!treeView1.Nodes.IsReadOnly && !treeView2.Nodes.IsReadOnly) { if(treeView1.SelectedNode != null) { TreeNode tn = treeView1.SelectedNode; treeView1.Nodes.Remove(tn); treeView2.Nodes.Insert(treeView2.Nodes.Count, tn); } } }
void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ ) { // If neither TreeNodeCollection is read-only, move the // selected node from treeView1 to treeView2. if ( !treeView1->Nodes->IsReadOnly && !treeView2->Nodes->IsReadOnly ) { if ( treeView1->SelectedNode != nullptr ) { TreeNode^ tn = treeView1->SelectedNode; treeView1->Nodes->Remove( tn ); treeView2->Nodes->Insert( treeView2->Nodes->Count, tn ); } } }
private void button1_Click(Object sender, EventArgs e) { // If neither TreeNodeCollection is read-only, move the // selected node from treeView1 to treeView2. if (!(treeView1.get_Nodes().get_IsReadOnly()) && !(treeView2.get_Nodes().get_IsReadOnly())) { if (treeView1.get_SelectedNode() != null) { TreeNode tn = treeView1.get_SelectedNode(); treeView1.get_Nodes().Remove(tn); treeView2.get_Nodes().Insert(treeView2.get_Nodes(). get_Count(), tn); } } } //button1_Click

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.Removeを検索する場合は、下記のリンクをクリックしてください。

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