Compartir vía


TreeNode Constructores

Definición

Inicializa una nueva instancia de la clase TreeNode.

Sobrecargas

TreeNode()

Inicializa una nueva instancia de la clase TreeNode.

TreeNode(String)

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta especificado.

TreeNode(SerializationInfo, StreamingContext)

Inicializa una nueva instancia de la clase TreeNode con la información de serialización y el contexto especificados.

TreeNode(String, TreeNode[])

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta y los nodos de árbol secundarios especificados.

TreeNode(String, Int32, Int32)

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta especificado y las imágenes que se han de mostrar cuando el nodo de árbol esté seleccionado o sin seleccionar.

TreeNode(String, Int32, Int32, TreeNode[])

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta, los nodos de árbol secundarios y las imágenes que se han especificado y se han de mostrar cuando el nodo de árbol esté seleccionado o sin seleccionar.

TreeNode()

Inicializa una nueva instancia de la clase TreeNode.

public:
 TreeNode();
public TreeNode ();
Public Sub New ()

Ejemplos

En el ejemplo de código siguiente se crea un nodo de árbol raíz al que asignar nodos de árbol secundarios. Se agrega un nodo de árbol secundario para cada Customer objeto de un ArrayList objeto al nodo de árbol raíz, así como a un nodo de árbol secundario para cada Order objeto asignado al Customer objeto. El Customer objeto se asigna a la Tag propiedad y los nodos de árbol que representan Customer objetos se muestran con Orange texto. En este ejemplo se requiere que tenga definido un Customer objeto y Order , un TreeView control en y Formun ArrayList denominado customerArray que contenga Customer objetos .

ref class Customer
{
public:
   ArrayList^ CustomerOrders;
   String^ CustomerName;
   Customer( String^ myName )
   {
      CustomerName = myName;
      CustomerOrders = gcnew ArrayList;
   }

};

ref class Order
{
public:
   String^ OrderID;
   Order( String^ myOrderID )
   {
      this->OrderID = myOrderID;
   }

};


   void AddRootNodes()
   {
      
      // Add a root node to assign the customer nodes to.
      TreeNode^ rootNode = gcnew TreeNode;
      rootNode->Text = "CustomerList";
      
      // Add a main root treenode.
      myTreeView->Nodes->Add( rootNode );
      
      // Add a root treenode for each 'Customer' object in the ArrayList.
      IEnumerator^ myEnum = customerArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);
         
         // Add a child treenode for each Order object.
         int i = 0;
         array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(5);
         IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Order^ myOrder = safe_cast<Order^>(myEnum->Current);
            myTreeNodeArray[ i ] = gcnew TreeNode( myOrder->OrderID );
            i++;
         }
         TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,myTreeNodeArray );
         
         // Display the customer names with and Orange font.
         customerNode->ForeColor = Color::Orange;
         
         // Store the Customer Object* in the Tag property of the TreeNode.
         customerNode->Tag = myCustomer;
         myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
      }
   }
public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList(); 
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

public void AddRootNodes()
{
   // Add a root node to assign the customer nodes to.
   TreeNode rootNode = new TreeNode();
   rootNode.Text = "CustomerList";
   // Add a main root treenode.
   myTreeView.Nodes.Add(rootNode);

   // Add a root treenode for each 'Customer' object in the ArrayList.
   foreach(Customer myCustomer in customerArray)
   {
      // Add a child treenode for each Order object.
      int i = 0;
      TreeNode[] myTreeNodeArray = new TreeNode[5];
      foreach(Order myOrder in myCustomer.CustomerOrders)
      {
         myTreeNodeArray[i] = new TreeNode(myOrder.OrderID);
         i++;
      }
      TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
        myTreeNodeArray);
        // Display the customer names with and Orange font.
        customerNode.ForeColor = Color.Orange;
        // Store the Customer object in the Tag property of the TreeNode.
        customerNode.Tag = myCustomer;
      myTreeView.Nodes[0].Nodes.Add(customerNode);
   }
}
Public Class Customer
   Public CustomerOrders As ArrayList
   Public CustomerName As String
   Public Sub New(myName As String)
      CustomerName = myName
      CustomerOrders = New ArrayList()
   End Sub
End Class

Public Class Order
   Public OrderID As String
   Public Sub New(myOrderID As String)
      Me.OrderID = myOrderID
   End Sub
End Class

Public Sub AddRootNodes()
   ' Add a root node to assign the customer nodes to.
   Dim rootNode As TreeNode
   rootNode = New TreeNode()
   rootNode.Text = "CustomerList"
   ' Add a main root treenode.
   myTreeView.Nodes.Add(rootNode)

   ' Add a root treenode for each Customer object in the ArrayList.
   Dim myCustomer As Customer
   For Each myCustomer In customerArray
      ' Add a child treenode for each Order object.
      Dim i As Integer = 0
      Dim myTreeNodeArray(4) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         myTreeNodeArray(i) = New TreeNode(myOrder.OrderID)
         i += 1
      Next myOrder
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
        myTreeNodeArray)
      ' Display the customer names with and Orange font.
      customerNode.ForeColor = Color.Orange
      ' Store the Customer object in the Tag property of the TreeNode.
      customerNode.Tag = myCustomer
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

Consulte también

Se aplica a

TreeNode(String)

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta especificado.

public:
 TreeNode(System::String ^ text);
public TreeNode (string text);
new System.Windows.Forms.TreeNode : string -> System.Windows.Forms.TreeNode
Public Sub New (text As String)

Parámetros

text
String

Etiqueta Text del nuevo nodo de árbol.

Ejemplos

En el ejemplo de código siguiente se muestra la información del cliente en un TreeView control . Los nodos de árbol raíz muestran los nombres de cliente y los nodos de árbol secundarios muestran los números de pedido asignados a cada cliente. En este ejemplo, se muestran 1000 clientes con 15 pedidos cada uno. La reintentos de TreeView se suprime mediante los BeginUpdate métodos y EndUpdate , y se muestra una espera Cursor mientras TreeView crea y pinta los TreeNode objetos. Este ejemplo requiere que tenga un Customer objeto que pueda contener una colección de Order objetos . También requiere que haya creado una instancia de un TreeView control en .Form

// The basic Customer class.
ref class Customer: public System::Object
{
private:
   String^ custName;

protected:
   ArrayList^ custOrders;

public:
   Customer( String^ customername )
   {
      custName = "";
      custOrders = gcnew ArrayList;
      this->custName = customername;
   }


   property String^ CustomerName 
   {
      String^ get()
      {
         return this->custName;
      }

      void set( String^ value )
      {
         this->custName = value;
      }

   }

   property ArrayList^ CustomerOrders 
   {
      ArrayList^ get()
      {
         return this->custOrders;
      }

   }

};


// End Customer class
// The basic customer Order class.
ref class Order: public System::Object
{
private:
   String^ ordID;

public:
   Order( String^ orderid )
   {
      ordID = "";
      this->ordID = orderid;
   }


   property String^ OrderID 
   {
      String^ get()
      {
         return this->ordID;
      }

      void set( String^ value )
      {
         this->ordID = value;
      }

   }

};
// End Order class



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.
   myEnum = customerArray->GetEnumerator();
   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();
}

// 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();
}
Public Class Customer
   Inherits [Object]
   Private custName As String = ""
   Friend custOrders As New ArrayList()

   Public Sub New(ByVal customername As String)
      Me.custName = customername
   End Sub

   Public Property CustomerName() As String
      Get
         Return Me.custName
      End Get
      Set(ByVal Value As String)
         Me.custName = Value
      End Set
   End Property

   Public ReadOnly Property CustomerOrders() As ArrayList
      Get
         Return Me.custOrders
      End Get
   End Property
End Class


Public Class Order
   Inherits [Object]
   Private ordID As String

   Public Sub New(ByVal orderid As String)
      Me.ordID = orderid
   End Sub

   Public Property OrderID() As String
      Get
         Return Me.ordID
      End Get
      Set(ByVal Value As String)
         Me.ordID = Value
      End Set
   End Property
End Class

' 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

Comentarios

El text valor del parámetro se asigna a la propiedad del Text nodo y se convierte en la etiqueta del nodo de árbol.

Consulte también

Se aplica a

TreeNode(SerializationInfo, StreamingContext)

Inicializa una nueva instancia de la clase TreeNode con la información de serialización y el contexto especificados.

protected:
 TreeNode(System::Runtime::Serialization::SerializationInfo ^ serializationInfo, System::Runtime::Serialization::StreamingContext context);
protected TreeNode (System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext context);
new System.Windows.Forms.TreeNode : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Windows.Forms.TreeNode
Protected Sub New (serializationInfo As SerializationInfo, context As StreamingContext)

Parámetros

serializationInfo
SerializationInfo

SerializationInfo que contiene los datos para deserializar la clase.

context
StreamingContext

Control StreamingContext que contiene el origen y el destino de la secuencia serializada.

Se aplica a

TreeNode(String, TreeNode[])

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta y los nodos de árbol secundarios especificados.

public:
 TreeNode(System::String ^ text, cli::array <System::Windows::Forms::TreeNode ^> ^ children);
public TreeNode (string text, System.Windows.Forms.TreeNode[] children);
new System.Windows.Forms.TreeNode : string * System.Windows.Forms.TreeNode[] -> System.Windows.Forms.TreeNode
Public Sub New (text As String, children As TreeNode())

Parámetros

text
String

Etiqueta Text del nuevo nodo de árbol.

children
TreeNode[]

Matriz de objetos TreeNode secundarios.

Ejemplos

En el ejemplo de código siguiente se crea un nodo de árbol raíz al que asignar nodos de árbol secundarios. Se agrega un nodo de árbol secundario para cada Customer objeto de un ArrayList objeto al nodo de árbol raíz, así como a un nodo de árbol secundario para cada Order objeto asignado al Customer objeto. El Customer objeto se asigna a la Tag propiedad y los nodos de árbol que representan Customer objetos se muestran con Orange texto. En este ejemplo se requiere que tenga definido un Customer objeto y Order , un TreeView control en y Formun ArrayList denominado customerArray que contenga Customer objetos .

ref class Customer
{
public:
   ArrayList^ CustomerOrders;
   String^ CustomerName;
   Customer( String^ myName )
   {
      CustomerName = myName;
      CustomerOrders = gcnew ArrayList;
   }

};

ref class Order
{
public:
   String^ OrderID;
   Order( String^ myOrderID )
   {
      this->OrderID = myOrderID;
   }

};


   void AddRootNodes()
   {
      
      // Add a root node to assign the customer nodes to.
      TreeNode^ rootNode = gcnew TreeNode;
      rootNode->Text = "CustomerList";
      
      // Add a main root treenode.
      myTreeView->Nodes->Add( rootNode );
      
      // Add a root treenode for each 'Customer' object in the ArrayList.
      IEnumerator^ myEnum = customerArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);
         
         // Add a child treenode for each Order object.
         int i = 0;
         array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(5);
         IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Order^ myOrder = safe_cast<Order^>(myEnum->Current);
            myTreeNodeArray[ i ] = gcnew TreeNode( myOrder->OrderID );
            i++;
         }
         TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,myTreeNodeArray );
         
         // Display the customer names with and Orange font.
         customerNode->ForeColor = Color::Orange;
         
         // Store the Customer Object* in the Tag property of the TreeNode.
         customerNode->Tag = myCustomer;
         myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
      }
   }
public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList(); 
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

public void AddRootNodes()
{
   // Add a root node to assign the customer nodes to.
   TreeNode rootNode = new TreeNode();
   rootNode.Text = "CustomerList";
   // Add a main root treenode.
   myTreeView.Nodes.Add(rootNode);

   // Add a root treenode for each 'Customer' object in the ArrayList.
   foreach(Customer myCustomer in customerArray)
   {
      // Add a child treenode for each Order object.
      int i = 0;
      TreeNode[] myTreeNodeArray = new TreeNode[5];
      foreach(Order myOrder in myCustomer.CustomerOrders)
      {
         myTreeNodeArray[i] = new TreeNode(myOrder.OrderID);
         i++;
      }
      TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
        myTreeNodeArray);
        // Display the customer names with and Orange font.
        customerNode.ForeColor = Color.Orange;
        // Store the Customer object in the Tag property of the TreeNode.
        customerNode.Tag = myCustomer;
      myTreeView.Nodes[0].Nodes.Add(customerNode);
   }
}
Public Class Customer
   Public CustomerOrders As ArrayList
   Public CustomerName As String
   Public Sub New(myName As String)
      CustomerName = myName
      CustomerOrders = New ArrayList()
   End Sub
End Class

Public Class Order
   Public OrderID As String
   Public Sub New(myOrderID As String)
      Me.OrderID = myOrderID
   End Sub
End Class

Public Sub AddRootNodes()
   ' Add a root node to assign the customer nodes to.
   Dim rootNode As TreeNode
   rootNode = New TreeNode()
   rootNode.Text = "CustomerList"
   ' Add a main root treenode.
   myTreeView.Nodes.Add(rootNode)

   ' Add a root treenode for each Customer object in the ArrayList.
   Dim myCustomer As Customer
   For Each myCustomer In customerArray
      ' Add a child treenode for each Order object.
      Dim i As Integer = 0
      Dim myTreeNodeArray(4) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         myTreeNodeArray(i) = New TreeNode(myOrder.OrderID)
         i += 1
      Next myOrder
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
        myTreeNodeArray)
      ' Display the customer names with and Orange font.
      customerNode.ForeColor = Color.Orange
      ' Store the Customer object in the Tag property of the TreeNode.
      customerNode.Tag = myCustomer
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

Comentarios

El text valor del parámetro se asigna a la propiedad del Text nodo y se convierte en la etiqueta del nodo de árbol. Los nodos de árbol contenidos en la children matriz se agregan al TreeNodeCollection objeto almacenado en la Nodes propiedad .

Consulte también

Se aplica a

TreeNode(String, Int32, Int32)

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta especificado y las imágenes que se han de mostrar cuando el nodo de árbol esté seleccionado o sin seleccionar.

public:
 TreeNode(System::String ^ text, int imageIndex, int selectedImageIndex);
public TreeNode (string text, int imageIndex, int selectedImageIndex);
new System.Windows.Forms.TreeNode : string * int * int -> System.Windows.Forms.TreeNode
Public Sub New (text As String, imageIndex As Integer, selectedImageIndex As Integer)

Parámetros

text
String

Etiqueta Text del nuevo nodo de árbol.

imageIndex
Int32

Valor del índice de la Image que se va a mostrar cuando el nodo de árbol no esté seleccionado.

selectedImageIndex
Int32

Valor del índice de la Image que se va a mostrar cuando el nodo de árbol esté seleccionado.

Ejemplos

En el ejemplo de código siguiente se crea y asigna un ImageList objeto a un TreeView control y se rellena el TreeView control con TreeNode objetos . A los nodos de árbol se les asignan imágenes de que ImageList se muestran cuando el nodo de árbol está en un estado seleccionado o no seleccionado. En este ejemplo se requiere que tenga un Form objeto que contenga un TreeViewobjeto y un ArrayList que contenga Customer objetos que contienen Order cada uno de ellos. También requiere que se definan los Customer objetos y Order .

ref class Customer
{
public:
   ArrayList^ CustomerOrders;
   String^ CustomerName;
   Customer( String^ myName )
   {
      CustomerName = myName;
      CustomerOrders = gcnew ArrayList;
   }

};

ref class Order
{
public:
   String^ OrderID;
   Order( String^ myOrderID )
   {
      this->OrderID = myOrderID;
   }

};

   void FillTreeView()
   {
      
      // Load the images in an ImageList.
      ImageList^ myImageList = gcnew ImageList;
      myImageList->Images->Add( Image::FromFile( "Default.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedDefault.gif" ) );
      myImageList->Images->Add( Image::FromFile( "Root.gif" ) );
      myImageList->Images->Add( Image::FromFile( "UnselectedCustomer.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedCustomer.gif" ) );
      myImageList->Images->Add( Image::FromFile( "UnselectedOrder.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedOrder.gif" ) );
      
      // Assign the ImageList to the TreeView.
      myTreeView->ImageList = myImageList;
      
      // Set the TreeView control's default image and selected image indexes.
      myTreeView->ImageIndex = 0;
      myTreeView->SelectedImageIndex = 1;
      
      /* Set the index of image from the
        ImageList for selected and unselected tree nodes.*/
      this->rootImageIndex = 2;
      this->selectedCustomerImageIndex = 3;
      this->unselectedCustomerImageIndex = 4;
      this->selectedOrderImageIndex = 5;
      this->unselectedOrderImageIndex = 6;
      
      // Create the root tree node.
      TreeNode^ rootNode = gcnew TreeNode( "CustomerList" );
      rootNode->ImageIndex = rootImageIndex;
      rootNode->SelectedImageIndex = rootImageIndex;
      
      // Add a main root tree node.
      myTreeView->Nodes->Add( rootNode );
      
      // Add a root tree node for each Customer object in the ArrayList.
      IEnumerator^ myEnum = customerArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);
         
         // Add a child tree node for each Order object.
         int countIndex = 0;
         array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myCustomer->CustomerOrders->Count);
         IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Order^ myOrder = safe_cast<Order^>(myEnum->Current);
            
            // Add the Order tree node to the array.
            myTreeNodeArray[ countIndex ] = gcnew TreeNode( myOrder->OrderID,unselectedOrderImageIndex,selectedOrderImageIndex );
            countIndex++;
         }
         TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,unselectedCustomerImageIndex,selectedCustomerImageIndex,myTreeNodeArray );
         myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
      }
   }

public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList(); 
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

private void FillTreeView()
{
    // Load the images in an ImageList.
    ImageList myImageList = new ImageList();
    myImageList.Images.Add(Image.FromFile("Default.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"));
    myImageList.Images.Add(Image.FromFile("Root.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"));
    
    // Assign the ImageList to the TreeView.
    myTreeView.ImageList = myImageList;
    
    // Set the TreeView control's default image and selected image indexes.
    myTreeView.ImageIndex = 0;
    myTreeView.SelectedImageIndex = 1;

    /* Set the index of image from the 
    ImageList for selected and unselected tree nodes.*/
    this.rootImageIndex = 2;
    this.selectedCustomerImageIndex = 3;
    this.unselectedCustomerImageIndex = 4;
    this.selectedOrderImageIndex = 5;
    this.unselectedOrderImageIndex = 6;
    
    // Create the root tree node.
    TreeNode rootNode = new TreeNode("CustomerList");
    rootNode.ImageIndex = rootImageIndex;
    rootNode.SelectedImageIndex = rootImageIndex;
      
    // Add a main root tree node.
    myTreeView.Nodes.Add(rootNode);

    // Add a root tree node for each Customer object in the ArrayList.
    foreach(Customer myCustomer in customerArray)
    {
        // Add a child tree node for each Order object.
        int countIndex=0;
        TreeNode[] myTreeNodeArray = new TreeNode[myCustomer.CustomerOrders.Count];
        foreach(Order myOrder in myCustomer.CustomerOrders)
        {
            // Add the Order tree node to the array.
            myTreeNodeArray[countIndex] = new TreeNode(myOrder.OrderID,
              unselectedOrderImageIndex, selectedOrderImageIndex);
            countIndex++;
        }
        // Add the Customer tree node.
        TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
            unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
        myTreeView.Nodes[0].Nodes.Add(customerNode);
    }
}
Public Class Customer
   Public CustomerOrders As ArrayList
   Public CustomerName As String
   
   Public Sub New(myName As String)
      CustomerName = myName
      CustomerOrders = New ArrayList()
   End Sub
End Class


Public Class Order
   Public OrderID As String
   
   Public Sub New(myOrderID As String)
      Me.OrderID = myOrderID
   End Sub
End Class

Private Sub FillTreeView()
   ' Load the images in an ImageList.
   Dim myImageList As New ImageList()
   myImageList.Images.Add(Image.FromFile("Default.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"))
   myImageList.Images.Add(Image.FromFile("Root.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"))
   
   ' Assign the ImageList to the TreeView.
   myTreeView.ImageList = myImageList
   
   ' Set the TreeView control's default image and selected image indexes.
   myTreeView.ImageIndex = 0
   myTreeView.SelectedImageIndex = 1
   
   ' Set the index of image from the 
   ' ImageList for selected and unselected tree nodes.
   Me.rootImageIndex = 2
   Me.selectedCustomerImageIndex = 3
   Me.unselectedCustomerImageIndex = 4
   Me.selectedOrderImageIndex = 5
   Me.unselectedOrderImageIndex = 6
   
   ' Create the root tree node.
   Dim rootNode As New TreeNode("CustomerList")
   rootNode.ImageIndex = rootImageIndex
   rootNode.SelectedImageIndex = rootImageIndex
   
   ' Add a main root tree node.
   myTreeView.Nodes.Add(rootNode)
   
   ' Add a root tree node for each Customer object in the ArrayList.
   Dim myCustomer As Customer
   For Each myCustomer In  customerArray
      ' Add a child tree node for each Order object.
      Dim countIndex As Integer = 0
      Dim myTreeNodeArray(myCustomer.CustomerOrders.Count) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         ' Add the Order tree node to the array.
         myTreeNodeArray(countIndex) = New TreeNode(myOrder.OrderID, _
            unselectedOrderImageIndex, selectedOrderImageIndex)
         countIndex += 1
      Next myOrder
      ' Add the Customer tree node.
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
         unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray)
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

Comentarios

El text valor del parámetro se asigna a la propiedad del Text nodo y se convierte en la etiqueta del nodo de árbol. Los imageIndex valores y selectedImageIndex son los valores de índice de un Image almacenado en el ImageList asignado a la TreeView.ImageList propiedad . La imagen a la que se hace referencia en la imageIndex propiedad se muestra cuando no se selecciona el nodo de árbol. Del mismo modo, la imagen a la que se hace referencia en la selectedImageIndex propiedad se muestra cuando el nodo de árbol está en un estado seleccionado.

Consulte también

Se aplica a

TreeNode(String, Int32, Int32, TreeNode[])

Inicializa una nueva instancia de la clase TreeNode con el texto de etiqueta, los nodos de árbol secundarios y las imágenes que se han especificado y se han de mostrar cuando el nodo de árbol esté seleccionado o sin seleccionar.

public:
 TreeNode(System::String ^ text, int imageIndex, int selectedImageIndex, cli::array <System::Windows::Forms::TreeNode ^> ^ children);
public TreeNode (string text, int imageIndex, int selectedImageIndex, System.Windows.Forms.TreeNode[] children);
new System.Windows.Forms.TreeNode : string * int * int * System.Windows.Forms.TreeNode[] -> System.Windows.Forms.TreeNode
Public Sub New (text As String, imageIndex As Integer, selectedImageIndex As Integer, children As TreeNode())

Parámetros

text
String

Etiqueta Text del nuevo nodo de árbol.

imageIndex
Int32

Valor del índice de la Image que se va a mostrar cuando el nodo de árbol no esté seleccionado.

selectedImageIndex
Int32

Valor del índice de la Image que se va a mostrar cuando el nodo de árbol esté seleccionado.

children
TreeNode[]

Matriz de objetos TreeNode secundarios.

Ejemplos

En el ejemplo de código siguiente se crea y asigna un ImageList objeto a un TreeView control y se rellena el TreeView control con TreeNode objetos . A los nodos de árbol se les asignan imágenes de que ImageList se muestran cuando el nodo de árbol está en un estado seleccionado o no seleccionado. En este ejemplo se requiere que tenga un Form objeto que contenga un TreeViewobjeto y un ArrayList que contenga Customer objetos que contienen Order cada uno de ellos. También requiere que se definan los Customer objetos y Order .

ref class Customer
{
public:
   ArrayList^ CustomerOrders;
   String^ CustomerName;
   Customer( String^ myName )
   {
      CustomerName = myName;
      CustomerOrders = gcnew ArrayList;
   }

};

ref class Order
{
public:
   String^ OrderID;
   Order( String^ myOrderID )
   {
      this->OrderID = myOrderID;
   }

};

   void FillTreeView()
   {
      
      // Load the images in an ImageList.
      ImageList^ myImageList = gcnew ImageList;
      myImageList->Images->Add( Image::FromFile( "Default.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedDefault.gif" ) );
      myImageList->Images->Add( Image::FromFile( "Root.gif" ) );
      myImageList->Images->Add( Image::FromFile( "UnselectedCustomer.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedCustomer.gif" ) );
      myImageList->Images->Add( Image::FromFile( "UnselectedOrder.gif" ) );
      myImageList->Images->Add( Image::FromFile( "SelectedOrder.gif" ) );
      
      // Assign the ImageList to the TreeView.
      myTreeView->ImageList = myImageList;
      
      // Set the TreeView control's default image and selected image indexes.
      myTreeView->ImageIndex = 0;
      myTreeView->SelectedImageIndex = 1;
      
      /* Set the index of image from the
        ImageList for selected and unselected tree nodes.*/
      this->rootImageIndex = 2;
      this->selectedCustomerImageIndex = 3;
      this->unselectedCustomerImageIndex = 4;
      this->selectedOrderImageIndex = 5;
      this->unselectedOrderImageIndex = 6;
      
      // Create the root tree node.
      TreeNode^ rootNode = gcnew TreeNode( "CustomerList" );
      rootNode->ImageIndex = rootImageIndex;
      rootNode->SelectedImageIndex = rootImageIndex;
      
      // Add a main root tree node.
      myTreeView->Nodes->Add( rootNode );
      
      // Add a root tree node for each Customer object in the ArrayList.
      IEnumerator^ myEnum = customerArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);
         
         // Add a child tree node for each Order object.
         int countIndex = 0;
         array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myCustomer->CustomerOrders->Count);
         IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Order^ myOrder = safe_cast<Order^>(myEnum->Current);
            
            // Add the Order tree node to the array.
            myTreeNodeArray[ countIndex ] = gcnew TreeNode( myOrder->OrderID,unselectedOrderImageIndex,selectedOrderImageIndex );
            countIndex++;
         }
         TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,unselectedCustomerImageIndex,selectedCustomerImageIndex,myTreeNodeArray );
         myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
      }
   }

public class Customer
{
   public ArrayList CustomerOrders;
   public string CustomerName;
   public Customer(string myName)
   {
      CustomerName = myName;
      CustomerOrders = new ArrayList(); 
   }
}
public class Order
{
   public string OrderID;
   public Order(string myOrderID )
   {
      this.OrderID = myOrderID;
   }
}

private void FillTreeView()
{
    // Load the images in an ImageList.
    ImageList myImageList = new ImageList();
    myImageList.Images.Add(Image.FromFile("Default.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"));
    myImageList.Images.Add(Image.FromFile("Root.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"));
    myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"));
    myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"));
    
    // Assign the ImageList to the TreeView.
    myTreeView.ImageList = myImageList;
    
    // Set the TreeView control's default image and selected image indexes.
    myTreeView.ImageIndex = 0;
    myTreeView.SelectedImageIndex = 1;

    /* Set the index of image from the 
    ImageList for selected and unselected tree nodes.*/
    this.rootImageIndex = 2;
    this.selectedCustomerImageIndex = 3;
    this.unselectedCustomerImageIndex = 4;
    this.selectedOrderImageIndex = 5;
    this.unselectedOrderImageIndex = 6;
    
    // Create the root tree node.
    TreeNode rootNode = new TreeNode("CustomerList");
    rootNode.ImageIndex = rootImageIndex;
    rootNode.SelectedImageIndex = rootImageIndex;
      
    // Add a main root tree node.
    myTreeView.Nodes.Add(rootNode);

    // Add a root tree node for each Customer object in the ArrayList.
    foreach(Customer myCustomer in customerArray)
    {
        // Add a child tree node for each Order object.
        int countIndex=0;
        TreeNode[] myTreeNodeArray = new TreeNode[myCustomer.CustomerOrders.Count];
        foreach(Order myOrder in myCustomer.CustomerOrders)
        {
            // Add the Order tree node to the array.
            myTreeNodeArray[countIndex] = new TreeNode(myOrder.OrderID,
              unselectedOrderImageIndex, selectedOrderImageIndex);
            countIndex++;
        }
        // Add the Customer tree node.
        TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
            unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
        myTreeView.Nodes[0].Nodes.Add(customerNode);
    }
}
Public Class Customer
   Public CustomerOrders As ArrayList
   Public CustomerName As String
   
   Public Sub New(myName As String)
      CustomerName = myName
      CustomerOrders = New ArrayList()
   End Sub
End Class


Public Class Order
   Public OrderID As String
   
   Public Sub New(myOrderID As String)
      Me.OrderID = myOrderID
   End Sub
End Class

Private Sub FillTreeView()
   ' Load the images in an ImageList.
   Dim myImageList As New ImageList()
   myImageList.Images.Add(Image.FromFile("Default.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedDefault.gif"))
   myImageList.Images.Add(Image.FromFile("Root.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedCustomer.gif"))
   myImageList.Images.Add(Image.FromFile("UnselectedOrder.gif"))
   myImageList.Images.Add(Image.FromFile("SelectedOrder.gif"))
   
   ' Assign the ImageList to the TreeView.
   myTreeView.ImageList = myImageList
   
   ' Set the TreeView control's default image and selected image indexes.
   myTreeView.ImageIndex = 0
   myTreeView.SelectedImageIndex = 1
   
   ' Set the index of image from the 
   ' ImageList for selected and unselected tree nodes.
   Me.rootImageIndex = 2
   Me.selectedCustomerImageIndex = 3
   Me.unselectedCustomerImageIndex = 4
   Me.selectedOrderImageIndex = 5
   Me.unselectedOrderImageIndex = 6
   
   ' Create the root tree node.
   Dim rootNode As New TreeNode("CustomerList")
   rootNode.ImageIndex = rootImageIndex
   rootNode.SelectedImageIndex = rootImageIndex
   
   ' Add a main root tree node.
   myTreeView.Nodes.Add(rootNode)
   
   ' Add a root tree node for each Customer object in the ArrayList.
   Dim myCustomer As Customer
   For Each myCustomer In  customerArray
      ' Add a child tree node for each Order object.
      Dim countIndex As Integer = 0
      Dim myTreeNodeArray(myCustomer.CustomerOrders.Count) As TreeNode
      Dim myOrder As Order
      For Each myOrder In  myCustomer.CustomerOrders
         ' Add the Order tree node to the array.
         myTreeNodeArray(countIndex) = New TreeNode(myOrder.OrderID, _
            unselectedOrderImageIndex, selectedOrderImageIndex)
         countIndex += 1
      Next myOrder
      ' Add the Customer tree node.
      Dim customerNode As New TreeNode(myCustomer.CustomerName, _
         unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray)
      myTreeView.Nodes(0).Nodes.Add(customerNode)
   Next myCustomer
End Sub

Comentarios

El text valor del parámetro se asigna a la propiedad del Text nodo y se convierte en la etiqueta del nodo de árbol. Los imageIndex valores y selectedImageIndex son los valores de índice de un Image almacenado en el ImageList asignado a la TreeView.ImageList propiedad . La imagen a la que se hace referencia en el imageIndex parámetro se muestra cuando no se selecciona el nodo de árbol. Del mismo modo, la imagen a la que se hace referencia en el selectedImageIndex parámetro se muestra cuando el nodo de árbol está en un estado seleccionado. Los nodos de árbol contenidos en la children matriz se agregan al TreeNodeCollection objeto almacenado en la Nodes propiedad .

Consulte también

Se aplica a