Windows Forms .NET 3.5 using resource images for a Treeview control
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?
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));
}
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);
}
精彩评论