Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add Images to tree nodes

Posted on 2010-11-30
5
Medium Priority
?
792 Views
Last Modified: 2012-05-10
I have a C# .net windows form application that creates a treeview from from a database.  There are 5 different NODES in the treeview and I want to assign a image to each one.  How can I do this?  

Treeview
  - --->   001   Image 1
  ----->  004   Image 2  
  ----->  005   Image 3
  ----->  001   Image 1  
 
I just want to know how to assign an image to a node.  something like treeGX1.SelectedNode.Image = "C:\dddddd.jpg"
 
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Expert Comment

by:ricovox
ID: 34241727
Hi Hojoformo,

You can do this by assigning an imagelist to the treeview and then assigning the desired image Key or index to each Node.

I will provide code in a followup post.
0
 
LVL 4

Accepted Solution

by:
ricovox earned 2000 total points
ID: 34241826
Assume you have added an ImageList "imgList" and a Treeview "tvwMain" to your form.

You can assign the ImageList to the Treeview in the VS designer by simply selecting the desired ImageList control from the "ImageList" property's drop-down box.

Or in code:
tvwMain.ImageList = imgList;

Open in new window


Then add all desired images to the ImageList. You can do this in the designer by adding resources, our you can do it in code:

imgList.Images.Add("key", imgImageOrIcon);

Open in new window


where "key" is any string key you want to associate with the image, and imgImageOrIcon is either an Icon object or an Image object that you can load from file using Image.FromFile(string path) etc.

Finally, assign the desired image to the treeview node:
//where key is the key you assigned when you added the image.
tvwMain.SelectedNode.ImageKey = "key"; 

Open in new window


you can also use the image index instead of key:
tvwMain.SelectedNode.ImageIndex = 3;

Open in new window


Hope that helps!
0
 
LVL 4

Expert Comment

by:ricovox
ID: 34241857
Again, just so you know, you can obtain an Image object to add to the ImageList by using the static Image.FromFile(string path) function.

Image image1 = Image.FromFile("C:\dddddd.jpg");
imgList.Images.Add("itemKey1", image1);

Open in new window

0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34242533
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34247592
Hello,
Try below code.

public ImageList ImageList { get; set; }


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


0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question