Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top