.NET Windows Forms 2.0: How to Create a Shared Image?

I want to have one imageList used by multiple forms in a project. In other words, multiple controls share the same list of images.

Note. Ideally, multiple projects in the same solution will use the same list of images, but I don't want to ask too much about Microsoft.

Some of the controls are listviews, some are tree views, some are custom controls, and some are controls that perform custom paint cycles.

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

Some of the problems:

  • Where to place the image list component? He must sit in some form
  • How can I convince the IDE to render SmallImageList-style controls that are in various forms as a list?
  • If I instead create an imagelist at runtime, how do I create the images that appear in the image list?

Note: This was easy in Delphi. You would drop the ImageList component as usual, and it just appears in the list of available image lists under the SmallImageList property in the Properties window.

Note. It's good to say that this cannot be done in Visual Studio. If the answer is that it cannot be done, that would be the accepted answer.

0


source to share


4 answers


As for the main question:

.NET Windows Forms 2.0: How to Create a Shared Image?



You can share ImageList with UserControl. Just add a UserControl to all of your forms and you should be able to access the ImageList.

+1


source


We have a multi-project WinForms application built from .NET 2.0.

One of the projects takes place:

  • all images used throughout the solution
  • 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 (mainly PNG) by name.



For example,

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

It is much easier to manage than with .NET 1.1. Now you need to add the image to the project, drag the image to the graphic assets display (in Project | Properties) and compile.

Since projects are reused for different solutions, we can use the same image library wherever we need.

Not sure if this really answers your question, but I hope this is helpful.

+1


source


I started Visual Studio and thought, "Let's create an extended ImageList that inherits from imagelist and fix that and post the solution on SO" and got hit with an error, the ImageList is sealed (unrecoverable), so I'm stuck before I even started.

But I remembered that sometime someone did it allready and google did the trick after tweaking the keywords a little.

I found a solution at CodeProject where Neil Andrews seems to have done exactly what you want, make ImageLists inheritable and global globally across multiple forms and controls with full design time support.

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

Thanks, Neil!

0


source


You can declare a Shared ImageList variable and populate it when you first start your application.

    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

      

0


source







All Articles