Share the maker in .Net Winforms

I want to have one copy of the list of images that I want to split into all the forms in my applications (toolbar icons). I saw the question asked earlier, and people came up with a user control (which is not very good, as it will create several instances of imagelist and thus create unnecessary objects and overhead).

Support for development time will be good, but not very important.

In Delphi, it was pretty simple: create a DataForm, share images, and you're off.

Is there a C # / option. Net / Winforms?

+3
source share
2 answers

ImageList , :

public static class ImageListWrapper
{
    static ImageListWrapper()
    {
        ImageList = new ImageList();
        LoadImages(ImageList);
    }

    private static void LoadImages(ImageList imageList)
    {
        // load images into the list
    }

    public static ImageList ImageList { get; private set; }
}

ImageList:

someControl.Image = ImageListWrapper.ImageList.Images["some_image"];

.

+5

(. ). , , - , .


using System.Windows.Forms;
using System.ComponentModel;

//use like this.ImageList = StaticImageList.Instance.GlobalImageList
//can use designer on this class but wouldn't want to drop it onto a design surface
[ToolboxItem(false)]
public class StaticImageList : Component
{
    private ImageList globalImageList;
    public ImageList GlobalImageList
    {
        get
        {
            return globalImageList;
        }
        set
        {
            globalImageList = value;
        }
    }

    private IContainer components;

    private static StaticImageList _instance;
    public static StaticImageList Instance
    {
        get
        {
            if (_instance == null) _instance = new StaticImageList();
            return _instance;
        }
    }

    private StaticImageList ()
        {
        InitializeComponent();
        }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.globalImageList = new System.Windows.Forms.ImageList(this.components);
        // 
        // GlobalImageList
        // 
        this.globalImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
        this.globalImageList.ImageSize = new System.Drawing.Size(16, 16);
        this.globalImageList.TransparentColor = System.Drawing.Color.Transparent;
    }
}
+3

Source: https://habr.com/ru/post/1712844/


All Articles