TreeView.Nodes プロパティ
メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。
TreeView コントロールのルート ノードを表す TreeNode オブジェクトのコレクションを取得します。
名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文

Nodes プロパティを使用して、ツリー内のすべてのルート ノードを格納する TreeNodeCollection オブジェクトを取得します。通常、このコレクションは、すべてのルート ノードをすばやく反復処理する場合や、ツリー内の特定のルート ノードにアクセスする場合に使用されます。
![]() |
---|
一般的なツリー構造のルート ノードは 1 つのみですが、TreeView コントロールに複数のルート ノードを追加することもできます。 |
Nodes プロパティは、ツリー内のルート ノードをプログラムによって管理する場合にも使用されます。コレクションの TreeNode オブジェクトを追加、挿入、削除、および取得できます。このコレクションに加えられた更新は、サーバーへの次のラウンド トリップの後で TreeView コントロールに自動的に反映されます。
ルート ノードの子ノードにアクセスするには、ルート ノードの ChildNodes プロパティを使用します。ノード レベルを下位に移動していくには、後続の各親レベルのノードの ChildNodes プロパティを使用します。

Nodes プロパティを使用してツリーを走査するコード例を次に示します。
<%@ 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


TreeView.Nodes プロパティ
ツリー ビュー コントロールに割り当てられているツリー ノードのコレクションを取得します。
名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
[LocalizableAttribute(true)] public: property TreeNodeCollection^ Nodes { TreeNodeCollection^ get (); }
プロパティ値
ツリー ビュー コントロールに割り当てられているツリー ノードを表す TreeNodeCollection。

Nodes プロパティは、TreeNode オブジェクトのコレクションを保持します。各オブジェクトには、それぞれの TreeNodeCollection を格納できる Nodes プロパティがあります。このようにツリー ノードを入れ子にできるため、ツリー構造での移動は難しくなることがありますが、FullPath プロパティを使用すると、ツリー構造内の位置を簡単に確認できます。

TreeView コントロールに顧客情報を表示するコード例を次に示します。ルート ツリー ノードに顧客名が表示され、各顧客に割り当てられた発注番号が子ツリー ノードに表示されます。この例では、1,000 人の顧客が表示され、顧客ごとに 15 の発注内容が示されます。BeginUpdate メソッドと EndUpdate メソッドを使用すると、TreeView は再描画されません。TreeView が TreeNode オブジェクトを作成して描画する間、待機 Cursor が表示されます。この例では、Order オブジェクトのコレクションを保持できる Customer オブジェクトが存在する必要があります。また、TreeView コントロールのインスタンスが Form 上に作成されている必要もあります。
' Create a new ArrayList to hold the Customer objects. Private customerArray As New ArrayList() Private Sub FillMyTreeView() ' Add customers to the ArrayList of Customer objects. Dim x As Integer For x = 0 To 999 customerArray.Add(New Customer("Customer" + x.ToString())) Next x ' Add orders to each Customer object in the ArrayList. Dim customer1 As Customer For Each customer1 In customerArray Dim y As Integer For y = 0 To 14 customer1.CustomerOrders.Add(New Order("Order" + y.ToString())) Next y Next customer1 ' Display a wait cursor while the TreeNodes are being created. Cursor.Current = New Cursor("MyWait.cur") ' Suppress repainting the TreeView until all the objects have been created. treeView1.BeginUpdate() ' Clear the TreeView each time the method is called. treeView1.Nodes.Clear() ' Add a root TreeNode for each Customer object in the ArrayList. Dim customer2 As Customer For Each customer2 In customerArray treeView1.Nodes.Add(New TreeNode(customer2.CustomerName)) ' Add a child TreeNode for each Order object in the current Customer object. Dim order1 As Order For Each order1 In customer2.CustomerOrders treeView1.Nodes(customerArray.IndexOf(customer2)).Nodes.Add( _ New TreeNode(customer2.CustomerName + "." + order1.OrderID)) Next order1 Next customer2 ' Reset the cursor to the default for all controls. Cursor.Current = System.Windows.Forms.Cursors.Default ' Begin repainting the TreeView. treeView1.EndUpdate() End Sub 'FillMyTreeView
// Create a new ArrayList to hold the Customer objects. private ArrayList customerArray = new ArrayList(); private void FillMyTreeView() { // Add customers to the ArrayList of Customer objects. for(int x=0; x<1000; x++) { customerArray.Add(new Customer("Customer" + x.ToString())); } // Add orders to each Customer object in the ArrayList. foreach(Customer customer1 in customerArray) { for(int y=0; y<15; y++) { customer1.CustomerOrders.Add(new Order("Order" + y.ToString())); } } // Display a wait cursor while the TreeNodes are being created. Cursor.Current = new Cursor("MyWait.cur"); // Suppress repainting the TreeView until all the objects have been created. treeView1.BeginUpdate(); // Clear the TreeView each time the method is called. treeView1.Nodes.Clear(); // Add a root TreeNode for each Customer object in the ArrayList. foreach(Customer customer2 in customerArray) { treeView1.Nodes.Add(new TreeNode(customer2.CustomerName)); // Add a child treenode for each Order object in the current Customer object. foreach(Order order1 in customer2.CustomerOrders) { treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add( new TreeNode(customer2.CustomerName + "." + order1.OrderID)); } } // Reset the cursor to the default for all controls. Cursor.Current = Cursors.Default; // Begin repainting the TreeView. treeView1.EndUpdate(); }
void FillMyTreeView() { // Add customers to the ArrayList of Customer objects. for ( int x = 0; x < 1000; x++ ) { customerArray->Add( gcnew Customer( "Customer " + x ) ); } // Add orders to each Customer object in the ArrayList. IEnumerator^ myEnum = customerArray->GetEnumerator(); while ( myEnum->MoveNext() ) { Customer^ customer1 = safe_cast<Customer^>(myEnum->Current); for ( int y = 0; y < 15; y++ ) { customer1->CustomerOrders->Add( gcnew Order( "Order " + y ) ); } } // Display a wait cursor while the TreeNodes are being created. ::Cursor::Current = gcnew System::Windows::Forms::Cursor( "MyWait.cur" ); // Suppress repainting the TreeView until all the objects have been created. treeView1->BeginUpdate(); // Clear the TreeView each time the method is called. treeView1->Nodes->Clear(); // Add a root TreeNode for each Customer object in the ArrayList. while ( myEnum->MoveNext() ) { Customer^ customer2 = safe_cast<Customer^>(myEnum->Current); treeView1->Nodes->Add( gcnew TreeNode( customer2->CustomerName ) ); // Add a child treenode for each Order object in the current Customer object. IEnumerator^ myEnum = customer2->CustomerOrders->GetEnumerator(); while ( myEnum->MoveNext() ) { Order^ order1 = safe_cast<Order^>(myEnum->Current); treeView1->Nodes[ customerArray->IndexOf( customer2 ) ]->Nodes->Add( gcnew TreeNode( customer2->CustomerName + "." + order1->OrderID ) ); } } // Reset the cursor to the default for all controls. ::Cursor::Current = Cursors::Default; // Begin repainting the TreeView. treeView1->EndUpdate(); }
// Create a new ArrayList to hold the Customer objects. private ArrayList customerArray = new ArrayList(); private void FillMyTreeView() { // Add customers to the ArrayList of Customer objects. for (int x = 0; x < 1000; x++) { customerArray.Add(new Customer("Customer" + ((Int32)x).ToString())); } // Add orders to each Customer object in the ArrayList. for (int iCtr = 0; iCtr < customerArray.get_Count(); iCtr++) { Customer customer1 = (Customer)customerArray.get_Item(iCtr); for (int y = 0; y < 15; y++) { customer1.get_CustomerOrders().Add(new Order("Order" + ((Int32)y).ToString())); } } // Display a wait cursor while the TreeNodes are being created. get_Cursor().set_Current(new Cursor("MyWait.cur")); // Suppress repainting the TreeView until all the objects have // been created. treeView1.BeginUpdate(); // Clear the TreeView each time the method is called. treeView1.get_Nodes().Clear(); // Add a root TreeNode for each Customer object in the ArrayList. for (int iCtr1 = 0; iCtr1 < customerArray.get_Count(); iCtr1++) { Customer customer2 = (Customer)customerArray.get_Item(iCtr1); treeView1.get_Nodes().Add(new TreeNode(customer2.get_CustomerName())); // Add a child treenode for each Order object in the current // Customer object. for (int iCtr2 = 0; iCtr2 < customer2.get_CustomerOrders(). get_Count(); iCtr2++) { Order order1 = (Order)customer2.get_CustomerOrders(). get_Item(iCtr2); treeView1.get_Nodes(). get_Item(customerArray.IndexOf(customer2)).get_Nodes(). Add(new TreeNode(customer2.get_CustomerName() + "." + order1.get_OrderID())); } } // Reset the cursor to the default for all controls. get_Cursor().set_Current(Cursors.get_Default()); // Begin repainting the TreeView. treeView1.EndUpdate(); } //FillMyTreeView

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

- TreeView.Nodesのページへのリンク