TreeNodeCollection.Count プロパティ
アセンブリ: System.Web (system.web.dll 内)


Count プロパティを使用して、TreeNodeCollection 内の項目の数を確認します。通常、このプロパティは、コレクションを反復処理してコレクションの上限を確認する場合に使用されます。Count プロパティは、コレクションが空であるかどうかを確認する手段としてもよく使用されます。

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>

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.Count プロパティ
アセンブリ: 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

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

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