R – .NET Windows Forms 2.0: How to have a shared imagelist

.netimagewinforms

I want have to have a single imageList used by multiple forms in a project. That is to say, multiple controls use the same image list.

Note: Ideally multiple projects in a single solution will use the same image list – but I don't want to ask too much of Microsoft at once.

Some controls are listview, some are treeviews, some are custom controls, some are controls that do custom paint cycles.

How can I point multiple ListViews and TreeViews to the same image list component?

Some of the issues involved are:

  • Where do I put the image list component? It has to sit on some sort of form
  • How do I convince the IDE to show imagelist controls in the "SmallImageList" that are on different forms as the listview?
  • If I instead construct the imagelist at runtime, how do I design the images that appear in the image list?

Note: This was easy in Delphi. You'd drop an ImageList component as you normally do, and it just appeared in the list of available image lists under the "SmallImageList" property in the Properties window.

Note: It's okay to say that it can't be done in Visual Studio. If the answer is that it cannot be done then that will be the accepted answer.

Best Solution

We have a multi-project WinForms app built out of .NET 2.0.

One of the projects holds:

  • all the images used across the whole solution
  • an ImageManager class, which has one static method, containing one line of code:

Return CType(My.Resources.ResourceManager.GetObject(name), Image)

Then we just refer to the images (mostly .PNGs) by name.

For instance,

button.Image = ImageManager.GetImage("CancelIcon")

This is all a lot easier to manage than it was with .NET 1.1. Now all you have to do is add the image to the project, drag the image on to the graphical resources display (in Project | Properties), and compile.

Since projects are reusable across solutions, we can use the same image library everywhere we need to.

Not sure if this really answers your question, but I hope it's helpful.