ListView Icon

ListView にアイコンを設定します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プログラムの説明

  1. ListView にアイコンを設定して表示します。
    C:\DATA\Test\ のフォルダーに Icon01.ico, Icon02.ico, Icon03.ico を格納しておいて下さい。
    プロジェクトの設定は Form を作成する を参照して下さい。
    完成したプログラムなので Command Line から Windows プログラムを実行 でもOKです。
    /**************************************************/
    /*★ ListView にアイコンを付けて表示    前田 稔 ★*/
    /**************************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        ListView    listView1;
        ImageList   imageListSmall;
        string[]    Files =
        { "C:\\DATA\\Test\\Icon01.ico", "C:\\DATA\\Test\\Icon02.ico", "C:\\DATA\\Test\\Icon03.ico" };
    
        public MyForm()
        {
            InitializeComponent();
            Load += new System.EventHandler(MyForm_Load);
        }
    
        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.SuspendLayout();
            // 
            // listView1
            // 
            this.listView1.Location = new System.Drawing.Point(12, 12);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(258, 232);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.List;
            //this.listView1.Sorted = true;
    
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(282, 256);
            this.Controls.Add(this.listView1);
            this.Name = "MyForm";
            this.ResumeLayout(false);
    
        }
    
        private void MyForm_Load(object sender, System.EventArgs e)
        {
            //Image List を作成
            imageListSmall = new ImageList();
            //imageListSmall.ImageSize = new Size(16, 16);
            for (int i = 0; i < Files.GetLength(0); i++)
            {   imageListSmall.Images.Add(new Bitmap(Files[i]));  }
            listView1.SmallImageList = imageListSmall;
    
            listView1.Items.Add("バラ");
            listView1.Items.Add("ヒマワリ");
            listView1.Items.Add("コスモス");
            listView1.Items[0].ImageIndex = 0;
            listView1.Items[1].ImageIndex = 1;
            listView1.Items[2].ImageIndex = 2;
        }
    }
    
    class form01
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. 3個のアイコンを ImageList に登録します。
    画像サイズの既定値は 16*16 に設定されています。
    ImageList は ImageList の基礎 を参照して下さい。
    ListView に ImageList を設定します。
        ImageList   imageListSmall;
        string[]    Files =
        { "C:\\DATA\\Test\\Icon01.ico", "C:\\DATA\\Test\\Icon02.ico", "C:\\DATA\\Test\\Icon03.ico" };
    
            for (int i = 0; i < Files.GetLength(0); i++)
            {   imageListSmall.Images.Add(new Bitmap(Files[i]));  }
            listView1.SmallImageList = imageListSmall;
        
  3. 個々の Item に ImageIndex を設定します。
            listView1.Items.Add("バラ");
            listView1.Items.Add("ヒマワリ");
            listView1.Items.Add("コスモス");
            listView1.Items[0].ImageIndex = 0;
            listView1.Items[1].ImageIndex = 1;
            listView1.Items[2].ImageIndex = 2;
        
  4. Windows(C++) でも同様のプログラムを作成しています。
    超初心者のプログラム入門(Windows)から List View にアイコンを設定 を参照して下さい。
    リンクがエラーになるときは「前田稔の超初心者のプログラム入門」から辿って下さい。

超初心者のプログラム入門(C# Frame Work)