TreeView Controls and Images

107 views
Skip to first unread message

Andy @ Corero

unread,
Mar 15, 2018, 7:15:24 AM3/15/18
to Migrated By Firefly
Hi

Is there a way you can specify the images for a TreeView control from embedded resources?

Obviously for an image, this is a simple case of
<nameofcontrol>.Image = <wherever>.Resources.<image name>
in the UI.cs file constructor

Now for a treeview, despite the image being a simple bmp file, you need to set
<nameofcontrol>.ImageList (I'd guess) = ????

(just entering the bmp gives a type mismatch)

(At the moment we are defining the ImageListLocation to be %WorkingDir%Images\<nameofimage>.bmp
which works, but are looking to embed all our remaining images to within a dll)

Thanks
Andy

Sefi Bar-El

unread,
Mar 20, 2018, 3:18:06 AM3/20/18
to Migrated By Firefly
Hi Andy,

You can set an image list and add images for the Collapsed and Expanded states of the tree nodes.

Here is a code sample (I've added 2 images named Folder and FolderOpen to the project resources):
namespace Northwind.Demo.Views
{
   
// This is the code behind of the form
   
partial class TreeViewDemoView : Shared.Theme.Controls.Form
   
{
       
TreeViewDemo _controller;
       
public TreeViewDemoView(TreeViewDemo controller)
       
{
            _controller
= controller;
           
InitializeComponent();
 
           
            treeView1
.Data = new TreeViewData(_controller.categories.CategoryName);
 
           
// adding embedded images to the treeview control
           
var imageList = new ImageList();
            imageList
.Images.Add(new Bitmap(Properties.Resources.Folder));
            imageList
.Images.Add(new Bitmap(Properties.Resources.FolderOpen));
            treeView1
.ImageList = imageList;
            treeView1
.SelectedCollapsedImageIndex = 0;
            treeView1
.SelectedExpandedImageIndex = 1;
       
}
   
}
}


Andy @ Corero

unread,
Mar 20, 2018, 10:32:04 AM3/20/18
to Migrated By Firefly
Thanks Sefi

I'll try this in due course.
I think there are 3 images though - expanded, collapsed and 'not applicable' (I'm certainly seeing 3 images and 3 states in the existing treeview.)

Sefi Bar-El

unread,
Mar 22, 2018, 9:15:23 AM3/22/18
to Migrated By Firefly
Hi Andy,

You can use expressions for the image indexes.
The Tree control has the following events:

  • BindCollapsedImageIndex
  • BindExpandedImageIndex
  • BindSelectedCollapsedImageIndex
  • BindSelectedExpandedImageIndex
You can set them using the Properties/Events sheet.
Here is a simple example of Binding the CollapsedImageIndex:



And the code of the event is:

private void treeView1_BindCollapsedImageIndex(object sender, IntBindingEventArgs e)
{
   
if (_controller.Emps.TitleOfCourtesy == "Mr.")
        e
.Value = 0;
   
else if (_controller.Emps.TitleOfCourtesy == "Ms.")
        e
.Value = 1;
   
else
        e
.Value = 2;
}

BTW, you can also drag an ImageList control from the ToolBox to the form and choose the images to embed from the context menu:

Reply all
Reply to author
Forward
0 new messages