TreeView.Nodes Proprietà

Definizione

Ottiene la raccolta dei nodi dell'albero assegnati al controllo di visualizzazione albero.

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

Valore della proprietà

Insieme TreeNodeCollection che rappresenta i nodi della struttura ad albero assegnati al controllo di visualizzazione ad albero.

Esempio

Nell'esempio di codice seguente vengono visualizzate le informazioni sui clienti in un TreeView controllo. I nodi dell'albero radice visualizzano i nomi dei clienti e i nodi dell'albero figlio visualizzano i numeri di ordine assegnati a ogni cliente. In questo esempio vengono visualizzati 1.000 clienti con 15 ordini. Il ripainting dell'oggetto TreeView viene eliminato usando i BeginUpdate metodi e EndUpdate e viene visualizzata un'attesa Cursor mentre TreeView crea e disegna gli TreeNode oggetti. In questo esempio è necessario disporre di un Customer oggetto che può contenere una raccolta di Order oggetti. È anche necessario che sia stata creata un'istanza di un TreeView controllo in un Formoggetto .

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

Commenti

La Nodes proprietà contiene un insieme di TreeNode oggetti, ognuno dei quali ha una Nodes proprietà che può contenere il proprio TreeNodeCollection. Questo annidamento dei nodi dell'albero può rendere difficile spostarsi in una struttura ad albero, ma la FullPath proprietà semplifica la determinazione della posizione all'interno della struttura ad albero.

Si applica a

Prodotto Versioni
.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

Vedi anche