views:

489

answers:

2

Hello SO,

When populating my treeview I would like to use the same images that I use in my toolbar etc which are stored in a resource file.

The treeview seems to on accept images via an image list.

I was thinking of reflecting and adding the resources to an image list on load...

How do you guyz n girlz generally do this?

A: 

I usually have an image list that I populate using images from the resource file. This can easily be done when initializing the form.

Example (with three images in Resources.resx, called one, two and three):

private void PopulateImageList()
{
    _treeViewImageList.Images.Add("one", Resources.one);
    _treeViewImageList.Images.Add("two", Resources.two);
    _treeViewImageList.Images.Add("three", Resources.three);
}
Fredrik Mörk
Do you have an example?
Anthony Johnston
Ok, that looks pretty straight forward, thanks Fredrik
Anthony Johnston
A: 

Just for completeness, that "sledge hammer" approach to add all images from a resource

foreach (var propertyInfo in
    typeof(Resources).GetProperties(BindingFlags.Static | BindingFlags.NonPublic)
        .Where(info => info.PropertyType == typeof (Bitmap))) {
                mainImageList.Images.Add(
                    propertyInfo.Name,
                    (Bitmap)propertyInfo.GetValue(null, null));
}
Anthony Johnston