views:

1001

answers:

4

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. Ideally multiple projects in multiple solutions will use the same image list - but i don't want to ask too much of Microsoft at once.

Some controls are listview, come 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 to 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.

+1  A: 

I can't answer all your question yet but here is for the principal question:

.NET WinForms 2.0: How to have a shared imagelist?

You can share the ImageList with a UserControl. Just add the UserControl in all your form and you will be able to access the ImageList.

Daok
Is there a trick that will allow you to access the image list at design time with the user control or a component?
Lasse V. Karlsen
+1  A: 

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.

ChrisA
Your solution combined with daoks would be a perfect answer to the question I think. Make a proxy-control that contains a imagelist with the added functionality to get its images from a Resourcemanager.
Stefan
This ImageManager class doesn't help you to use the shared imagelist as an imagelist, does it? The listview and treeivew's aren't going to know how to call ImageManager.GetImage(imageIndex)
Ian Boyd
A: 

I fired up Visual Studio and thought "lets make a enhanced ImageList control that inherits from the imagelist and fix this and post a solution on SO" and was let down with a bang, the ImageList control is sealed (noninheritable) so I got stuck before I even started.

But I remembered that somwhere someone had done this allready and google did the trick after a bit of tweaking of the keywords.

I found a solution on CodeProject, where Neal Andrews seems to have made exactly what you want, make a ImageLists to be inherited and shared globaly across multiple forms and controls, with full design time support.

http://www.codeproject.com/KB/cpp/SharedImageListsComponent.aspx

Thanks Neal!

Stefan
That's more trouble than it's worth. An unsupported, non-zero maintenance, WSOD occasionally causing addon that every developer has to install into their IDE.
Ian Boyd
It does what you want and is probably the only way around the problem if you want to meet those criterias you set up (design support, shared gobaly across multiple forma and controls) so I would take it, and put a days work to get it perfected if I was u.
Stefan
A: 

Hi Ian. May be declaring a Shared ImageList variable and filling it the first time the application runs.

    Shared shoImageList As New ImageList
    Public Sub New()
        If shoImageList.Images.Count = 0 Then
            shoImageList = ImageList1 
            'or may be a For Each loop here'
        End If
        myTreeView.ImageList = shoImageList
        ImageList1 = Nothing
    End Sub
Ivan Ferrer Villa