Imagenes en Nodos de un TreeView

23/09/2003 - 04:20 por Fernando Navarro | Informe spam
Hola a todos;

Tengo un pequeño problema en el cual tengo un TreeView el
cual construyo dinamicamente por acceso a una base de
datos.

Lo que necesito saber es como ponerle imagenes a los
subnodos.

Porque solamente se los puedo poner a los nodos Padre.

Alguien sabe como hacer esto??

Saludos

Fernando Navarro
 

Leer las respuestas

#1 Leonardo Moreno Forero
23/09/2003 - 05:47 | Informe spam
Hola Fernando,

El siguiente evento, sacado de la documentación del framework, muestra como
asignarle imágenes a un control TreeView. Al igual que en VB 6.0 lo haces
con un objeto ImageList;
[Visual Basic]
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
[C#]
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);
}
}Atentamente,

Leonardo Moreno Forero
MCP - MCSD
Bogotá - Colombia

"Fernando Navarro" escribió en el mensaje
news:0a3c01c38179$3d2385f0$
Hola a todos;

Tengo un pequeño problema en el cual tengo un TreeView el
cual construyo dinamicamente por acceso a una base de
datos.

Lo que necesito saber es como ponerle imagenes a los
subnodos.

Porque solamente se los puedo poner a los nodos Padre.

Alguien sabe como hacer esto??

Saludos

Fernando Navarro

Preguntas similares