次の方法で共有


TreeNodeCollection.Add メソッド (TreeNode)

以前に作成したツリー ノードをツリー ノード コレクションの末尾に追加します。

Overloads Public Overridable Function Add( _
   ByVal node As TreeNode _) As Integer
[C#]
public virtual int Add(TreeNodenode);
[C++]
public: virtual int Add(TreeNode* node);
[JScript]
public function Add(
   node : TreeNode) : int;

パラメータ

  • node
    コレクションに追加する TreeNode

戻り値

ツリー ノード コレクションに追加される TreeNode の 0 から始まるインデックス値。

例外

例外の種類 条件
Exception node が、現在別の TreeView コントロールに割り当てられています。

解説

この形式の Add メソッドを使用すると、以前に作成した TreeNode オブジェクトをツリー ノードの末尾に追加できます。

新しい TreeNode オブジェクトをコレクションに追加する別の手段として、 AddRange メソッドまたは Insert メソッドも使用できます。

追加した TreeNode を削除するには、 Remove メソッド、 RemoveAt メソッド、または Clear メソッドを使用します。

メモ    TreeNode オブジェクトは、一度に 1 つの TreeView コントロールにしか割り当てることができません。ツリー ノードを新しいツリー ビュー コントロールに追加するには、まず現在のツリー ビューからそのツリー ノードを削除するか、そのノードのクローンを作成する必要があります。

使用例

[Visual Basic, C#, C++] TreeView コントロールに顧客情報を表示する例を次に示します。ルート ツリー ノードに顧客名が表示され、各顧客に割り当てられた発注番号が子ツリー ノードに表示されます。この例では、1,000 人の顧客が表示され、顧客ごとに 15 の発注内容が示されます。 BeginUpdate メソッドと EndUpdate メソッドを使用すると、 TreeView は再描画されません。 TreeViewTreeNode オブジェクトを作成して描画する間、待機 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

[C#] 
// 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();
}

[C++] 
void FillMyTreeView() {
    // Add customers to the ArrayList of Customer objects.
    for (int x=0; x<1000; x++) {
        customerArray->Add(new Customer(String::Concat(S"Customer ", __box(x))));
    }

    // Add orders to each Customer object in the ArrayList.
    IEnumerator* myEnum = customerArray->GetEnumerator();
    while (myEnum->MoveNext()) {
        Customer* customer1 = __try_cast<Customer*>(myEnum->Current);

        for (int y=0; y<15; y++) {
            customer1->CustomerOrders->Add(new Order(String::Concat(S"Order ", __box(y))));
        }
    }

    // Display a wait cursor while the TreeNodes are being created.
    Cursor::Current = new System::Windows::Forms::Cursor(S"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 = __try_cast<Customer*>(myEnum->Current);

        treeView1->Nodes->Add(new 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 = __try_cast<Order*>(myEnum->Current);

            treeView1->Nodes->Item[customerArray->IndexOf(customer2)]->Nodes->Add(
                new TreeNode(String::Concat(customer2->CustomerName, S".", order1->OrderID)));
        }
    }

    // Reset the cursor to the default for all controls.
    Cursor::Current = Cursors::Default;

    // Begin repainting the TreeView.
    treeView1->EndUpdate();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

TreeNodeCollection クラス | TreeNodeCollection メンバ | System.Windows.Forms 名前空間 | TreeNodeCollection.Add オーバーロードの一覧 | Remove | RemoveAt