Replace an Image with another Image in an ImageList : ImageList « GUI Windows Forms « C# / CSharp Tutorial






Replace an Image with another Image in an ImageList
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

public class ImageListImageReplace : Form
{
  ImageList imgList;
  Button btn;

  public ImageListImageReplace()
  {
       Size = new Size(300,300);

    imgList = new ImageList();
    Image img;

    String[] arFiles = {"1.ico","2.ico","3.ico"};


    for (int i = 0; i < arFiles.Length; i++)
    {
      img = Image.FromFile(arFiles[i]);
      imgList.Images.Add(img);
    }

    imgList.ImageSize = new Size(32, 32);
    img = Image.FromFile("4.ico");
    imgList.Images[imgList.Images.Count - 1] = img;

    btn = new Button();
    btn.Parent = this;
    btn.ImageList = imgList;
    btn.ImageIndex = imgList.Images.Count - 1;
    btn.Location = new Point(0, 20);
    btn.Size = new Size(3 * imgList.ImageSize.Width, 
              2 * imgList.ImageSize.Height);

    btn.ImageIndex = 2;

  }

  static void Main() 
  {
      Application.Run(new ImageListImageReplace());
  }

}








23.67.ImageList
23.67.1.Add Image in an ImageList to a ButtonAdd Image in an ImageList to a Button
23.67.2.Replace an Image with another Image in an ImageListReplace an Image with another Image in an ImageList