TreeNodeCollection クラス

定義

TreeNode コントロールに含まれる TreeView オブジェクトのコレクションを表します。 このクラスは継承できません。

public ref class TreeNodeCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class TreeNodeCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type TreeNodeCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class TreeNodeCollection
Implements ICollection, IStateManager
継承
TreeNodeCollection
実装

次の例では、プログラムによってノードを追加し、ノードを削除する方法を TreeNodeCollection示します。 プロパティがChildNodesオブジェクトをNodesTreeNodeCollection返していることに注意してください。


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "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="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "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>

注釈

このTreeNodeCollectionクラスは、コントロール内TreeViewのオブジェクトのTreeNodeコレクションを格納および管理するために使用されます。 コントロールは TreeViewTreeNodeCollection 2 つのプロパティでクラスを使用します。 ルート ノードがプロパティに格納され、 Nodes 選択したノードがプロパティに CheckedNodes 格納されます。 コレクションは TreeNodeCollection 、子ノードを ChildNodes 格納するプロパティにも使用されます (存在する場合)。

このクラスでは TreeNodeCollection 、コレクション内の項目にアクセスするいくつかの方法がサポートされています。

  • インデクサーを Item[] 使用して、特定の TreeNode 0 から始まるインデックスでオブジェクトを直接取得します。

  • このメソッドを GetEnumerator 使用して、コレクションを反復処理するために使用できる列挙子を作成します。

  • (C#) または For Each (Visual Basic) を使用foreachしてコレクションを反復処理します。

  • このメソッドを CopyTo 使用して、コレクションの内容をオブジェクトに Array コピーします。

オブジェクトを追加および削除TreeNodeすることで、TreeNodeCollectionプログラムで管理できます。 コレクションにノードを追加するには、or AddAt メソッドをAdd使用します。 コレクションからノードを削除するには、、RemoveAtまたはClearメソッドをRemove使用します。

注意

コントロールが TreeView データ ソースにバインドされると、 Nodes バインドが行われるたびに、コレクションと ChildNodes コレクションが自動的に設定されます。 バインド間のコレクションに対する変更はすべて失われます。 これらの変更を保持するには、データ ソースを更新するか、バインドするたびにコレクションを手動で再構築します。

コレクション TreeNodeCollection 自体に関する情報を取得できるプロパティとメソッドが含まれています。 コレクション内の項目の数を調べるには、プロパティを Count 使用します。 コレクションに特定 TreeNode のオブジェクトが含まれているかどうかを判断する場合は、メソッドを Contains 使用します。 コレクション内のオブジェクトのインデックスを TreeNode 取得するには、メソッドを IndexOf 使用します。

コンストラクター

TreeNodeCollection()

TreeNodeCollection クラスの新しいインスタンスを既定値で初期化します。

TreeNodeCollection(TreeNode)

親ノード (または所有者) を指定して、TreeNodeCollection クラスの新しいインスタンスを初期化します。

プロパティ

Count

TreeNodeCollection オブジェクト内の項目の数を取得します。

IsSynchronized

TreeNodeCollection へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

Item[Int32]

TreeNode オブジェクト内の指定したインデックス位置にある TreeNodeCollection オブジェクトを取得します。

SyncRoot

TreeNodeCollection オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

メソッド

Add(TreeNode)

指定した TreeNode オブジェクトを TreeNodeCollection オブジェクトの末尾に追加します。

AddAt(Int32, TreeNode)

指定した TreeNode オブジェクトを、TreeNodeCollection オブジェクトの指定したインデックス位置に挿入します。

Clear()

TreeNodeCollection オブジェクトを空にします。

Contains(TreeNode)

指定した TreeNode オブジェクトがコレクション内にあるかどうかを確認します。

CopyTo(TreeNode[], Int32)

TreeNodeCollection のすべての項目を互換性のある 1 次元の TreeNode オブジェクト配列にコピーします。コピー先の配列の指定したインデックスからコピーが開始されます。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

TreeNodeCollection オブジェクトを反復処理するために使用できる列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(TreeNode)

指定した TreeNode オブジェクトのインデックスを確認します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(TreeNode)

指定した TreeNode オブジェクトを TreeNodeCollection オブジェクトから削除します。

RemoveAt(Int32)

指定したインデックス位置にある TreeNode オブジェクトを TreeNodeCollection オブジェクトから削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

TreeNodeCollection オブジェクトのすべての項目を、互換性のある 1 次元の Array にコピーします。コピー先の配列の指定したインデックスからコピーが開始されます。

IStateManager.IsTrackingViewState

TreeNodeCollection オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

IStateManager.LoadViewState(Object)

TreeNodeCollection オブジェクトが前回保存したビューステートを読み込みます。

IStateManager.SaveViewState()

ビューステートへの変更を Object に保存します。

IStateManager.TrackViewState()

ビューステートへの変更を追跡するように TreeNodeCollection に指示します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください