TreeNodeCollection.Countとは? わかりやすく解説

TreeNodeCollection.Count プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

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

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

解説解説
使用例使用例

Count プロパティ使用してTreeNodeCollection 内の項目の数を確認する方法の例を次に示します。この例では、次に TreeView コントロール先行順走査を実行してノードテキスト表示します

<%@ Page Language="VB" %>

<script runat="server">

  Sub Page_Load(ByVal sender As
 Object, ByVal e As EventArgs)

    ' If the TreeView control contains any root nodes, perform a
    ' preorder traversal of the tree and display the text of each node.
    If LinksTreeView.Nodes.Count > 0 Then

      ' Iterate through the root nodes in the Nodes property.
      Dim i As Integer

      For i = 0 To LinksTreeView.Nodes.Count
 - 1

        ' Display the nodes.
        DisplayChildNodeText(LinksTreeView.Nodes(i))

      Next i

    Else

      Message.Text = "The TreeView control does not have any nodes."

    End If

  End Sub

  Sub DisplayChildNodeText(ByVal node As
 TreeNode)

    ' Display the node's text value.
    Message.Text &= node.Text & "<br>"

    ' Iterate through the child nodes of the parent node passed into
    ' this method and display their values.
    Dim i As Integer

    For i = 0 To node.ChildNodes.Count - 1

      ' Recursively call the DisplayChildNodeText method to
      ' traverse the tree and display all the child nodes.
      DisplayChildNodeText(node.ChildNodes(i))

    Next i

  End Sub

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeNodeCollection Count 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="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>
                
                </asp:TreeNode>
                <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>
      
      <br><br>
      
      <asp:Label id="Message"
        runat="server"/>

    </form>
  </body>
</html>

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

<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // If the TreeView control contains any root nodes, perform a
    // preorder traversal of the tree and display the text of each node.
    if(LinksTreeView.Nodes.Count > 0)
    {

      // Iterate through the root nodes in the Nodes property.
      for(int i=0; i<LinksTreeView.Nodes.Count;
 i++)
      {

        // Display the nodes.
        DisplayChildNodeText(LinksTreeView.Nodes[i]);

      }
      
    }
    else
    {
    
      Message.Text = "The TreeView control does not have any nodes.";
    
    }

  }

  void DisplayChildNodeText(TreeNode node)
  {
  
    // Display the node's text value.
    Message.Text += node.Text + "<br>";

    // Iterate through the child nodes of the parent node passed into
    // this method and display their values.
    for(int i=0; i<node.ChildNodes.Count;
 i++)
    {

      // Recursively call the DisplayChildNodeText method to
      // traverse the tree and display all the child nodes.
      DisplayChildNodeText(node.ChildNodes[i]);

    }

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeNodeCollection Count 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="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>
                
                </asp:TreeNode>
                <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>
      
      <br><br>
      
      <asp:Label id="Message"
        runat="server"/>

    </form>
  </body>
</html>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TreeNodeCollection クラス
TreeNodeCollection メンバ
System.Web.UI.WebControls 名前空間
TreeView
ArrayList.Count
Nodes
CheckedNodes
TreeNode クラス
TreeNode.ChildNodes プロパティ

TreeNodeCollection.Count プロパティ

コレクション内の TreeNode オブジェクト合計数を取得します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

解説解説

Count プロパティは、コレクション割り当てられている TreeNode オブジェクトの数を保持しますコレクション反復処理するときに、ループの上限として Count プロパティ値を使用できます

メモメモ

コレクションインデックス値は 0 から始まるため、この数値から 1 を引いた数値ループの上限として使用する必要があります。この点を考慮しないと、コレクションの上限を超えて処理することになり、IndexOutOfRangeException 例外スローさます。

使用例使用例

TreeNodeCollection の TreeNode オブジェクトの数を表示しコレクション内容Object 配列コピーしてツリー ノードリストLabel コントロール表示するコード例次に示します。この例は、少なくとも 1 つTreeNode が TreeView の TreeNodeCollection にあること、および Label コントロールForm配置されていることを前提にしています。

Private Sub CopyTreeNodes()
   ' Get the collection of TreeNodes.
   Dim myNodeCollection As TreeNodeCollection
 = myTreeView.Nodes
   Dim myCount As Integer
 = myNodeCollection.Count

   myLabel.Text += "Number of nodes in the collection :"
 + myCount.ToString()

   myLabel.Text += ControlChars.NewLine + ControlChars.NewLine + _
     "Elements of the Array after Copying from the collection
 :" + ControlChars.NewLine

   ' Create an Object array.
   Dim myArray(myCount -1) As Object

   ' Copy the collection into an array.
   myNodeCollection.CopyTo(myArray, 0)
   Dim i As Integer
   For i = 0 To myArray.Length - 1
      myLabel.Text += CType(myArray(i), TreeNode).Text + ControlChars.NewLine
   Next i
End Sub
private void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection myNodeCollection = myTreeView.Nodes;
   int myCount = myNodeCollection.Count;

   myLabel.Text += "Number of nodes in the collection :"
 + myCount;
   myLabel.Text += "\n\nElements of the Array after Copying from the collection
 :\n";
   // Create an Object array.
   Object[] myArray = new Object[myCount];
   // Copy the collection into an array.
   myNodeCollection.CopyTo(myArray,0);
   for(int i=0; i<myArray.Length; i++)
   {
      myLabel.Text += ((TreeNode)myArray[i]).Text + "\n";
   }
}
void CopyTreeNodes()
{
   // Get the collection of TreeNodes.
   TreeNodeCollection^ myNodeCollection = myTreeView->Nodes;
   int myCount = myNodeCollection->Count;
   myLabel->Text = String::Concat( myLabel->Text, "Number of nodes in
 the collection : ", myCount );
   myLabel->Text = String::Concat( myLabel->Text, "\n\nElements of the
 Array after Copying from the collection :\n" );
   
   // Create an Object array.
   array<Object^>^myArray = gcnew array<Object^>(myCount);
   
   // Copy the collection into an array.
   myNodeCollection->CopyTo( myArray, 0 );
   for ( int i = 0; i < myArray->Length;
 i++ )
   {
      myLabel->Text = myLabel->Text + (dynamic_cast<TreeNode^>(myArray[
 i ]))->Text + "\n";

   }
}
private void CopyTreeNodes()
{
    // Get the collection of TreeNodes.
    TreeNodeCollection myNodeCollection = myTreeView.get_Nodes();
    int myCount = myNodeCollection.get_Count();

    myLabel.set_Text(myLabel.get_Text()
        + "Number of nodes in the collection :" + myCount);
    myLabel.set_Text(myLabel.get_Text()
        + "\n\nElements of the Array after Copying from the collection :\n");
    // Create an Object array.
    Object myArray[] = new Object[myCount];
    // Copy the collection into an array.
    myNodeCollection.CopyTo(myArray, 0);
    for (int i = 0; i < myArray.length;
 i++) {
        myLabel.set_Text(myLabel.get_Text()
            + ((TreeNode)myArray[i]).get_Text() + "\n");
    }
} //CopyTreeNodes
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「TreeNodeCollection.Count」の関連用語

TreeNodeCollection.Countのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TreeNodeCollection.Countのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS