• 0

C# .Net Adding images to specific treeview items


Question

Hey,

I have a treeview control, that list all the image files found in the image folder. For each item in the treeview, I want it to have a thumbnail preview of the image. I have sort of got it working, but it's only loading one image, so all the items are using the same image. The code is below, have I done anything wrong?

I am trying to do the same idea as you see in MSN Messenger 6, when adding a new display picture.

private void frm1_Load(object sender, EventArgs e)
 ? ? ? ?{
 ? ? ? ? ? ?ImageList li = new ImageList();
 ? ? ? ? ? ?li.ImageSize = new Size(32, 32);
 ? ? ? ? ? ?li.ColorDepth = ColorDepth.Depth32Bit;
 ? ? ? ? ? ?treeView1.ImageList = li;

 ? ? ? ? ? ?System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Application.StartupPath + @"\images");
 ? ? ? ? ? ?foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?li.Images.Add(Image.FromFile(@"imagesfile.Name));
 ? ? ? ? ? ? ? ?treeView1.Nodes.Add(null, file.Name, file.Name.ToString(), file.Name.ToString());
 ? ? ? ? ? ?}
 ? ? ? ?}

Thanks

4 answers to this question

Recommended Posts

  • 0
  Dan C said:
Hey,

I have a treeview control, that list all the image files found in the image folder. For each item in the treeview, I want it to have a thumbnail preview of the image. I have sort of got it working, but it's only loading one image, so all the items are using the same image. The code is below, have I done anything wrong?

I am trying to do the same idea as you see in MSN Messenger 6, when adding a new display picture.

private void frm1_Load(object sender, EventArgs e)
        {
            ImageList li = new ImageList();
            li.ImageSize = new Size(32, 32);
            li.ColorDepth = ColorDepth.Depth32Bit;
            treeView1.ImageList = li;

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Application.StartupPath + @"\images");
            foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
            {
                li.Images.Add(Image.FromFile(@"imagesfile.Name));
                treeView1.Nodes.Add(null, file.Name, file.Name.ToString(), file.Name.ToString());
            }
        }

Thanks

585504567[/snapback]

what treeview are you using? your Add method signature doesn't look like the included treeview in .NET.

Just a guess, though. You probably need to set each node's ImageIndex.

  • 0
  weenur said:
what treeview are you using? your Add method signature doesn't look like the included treeview in .NET.

Just a guess, though. You probably need to set each node's ImageIndex.

585505150[/snapback]

I am using the treeview control from .Net 2.0. Should of have mentioned that I am using framework 2.0.

  • 0
  Dan C said:
I am using the treeview control from .Net 2.0. Should of have mentioned that I am using framework 2.0.

585505164[/snapback]

Ah, ok.

It makes sense now. So you're trying to setting the ImageKey of the node to the name of the file, but you aren't setting the ImageKey to the file name when you add the image to the ImageList.

// should be
                li.Images.Add(file.Name, Image.FromFile(@"images\" + file.Name));
               treeView1.Nodes.Add(null, file.Name, file.Name.ToString(), file.Name.ToString());

  • 0
  weenur said:
Ah, ok.

It makes sense now. So you're trying to setting the ImageKey of the node to the name of the file, but you aren't setting the ImageKey to the file name when you add the image to the ImageList.

// should be
 ? ? ? ? ? ? ? ?li.Images.Add(file.Name, Image.FromFile(@"images\" + file.Name));
 ? ? ? ? ? ? ? treeView1.Nodes.Add(null, file.Name, file.Name.ToString(), file.Name.ToString());

585505233[/snapback]

Sweet, thanks:Dor that mate :D I have been struggling with this all day.

Tha:)s alot again :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.