英語で読む

次の方法で共有


TreeView.Nodes プロパティ

定義

ツリー ビュー コントロールに割り当てられているツリー ノードのコレクションを取得します。

C#
public System.Windows.Forms.TreeNodeCollection Nodes { get; }

プロパティ値

ツリー ビュー コントロールに割り当てられているツリー ノードを表す TreeNodeCollection

次のコード例では、コントロールに顧客情報を TreeView 表示します。 ルート ツリー ノードには顧客名が表示され、子ツリー ノードには各顧客に割り当てられた注文番号が表示されます。 この例では、1,000 人の顧客がそれぞれ 15 件の注文と共に表示されます。 メソッドと EndUpdate メソッドを使用BeginUpdateすると、 TreeView の再描画が抑制され、 がオブジェクトを作成して塗りつぶす間にTreeView待機CursorTreeNode表示されます。 この例では、オブジェクトのOrderコレクションをCustomer保持できる オブジェクトが必要です。 また、 にコントロールFormのインスタンスをTreeView作成している必要もあります。

C#

// The basic Customer class.
public class Customer : System.Object
{
   private string custName = "";
   protected ArrayList custOrders = new ArrayList();

   public Customer(string customername)
   {
      this.custName = customername;
   }

   public string CustomerName
   {      
      get{return this.custName;}
      set{this.custName = value;}
   }

   public ArrayList CustomerOrders 
   {
      get{return this.custOrders;}
   }
} // End Customer class 

// The basic customer Order class.
public class Order : System.Object
{
   private string ordID = "";

   public Order(string orderid)
   {
      this.ordID = orderid;
   }

   public string OrderID
   {      
      get{return this.ordID;}
      set{this.ordID = value;}
   }
} // End Order class

// 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();
}

注釈

プロパティは Nodes オブジェクトの TreeNode コレクションを保持します。各オブジェクトには Nodes 、独自 TreeNodeCollectionの を含めることができる プロパティがあります。 ツリー ノードを入れ子にすると、ツリー構造内の移動が困難になる可能性がありますが、 プロパティを FullPath 使用すると、ツリー構造内の位置を簡単に判断できます。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください