Hi, I am trying to set it up so that when the user wants to, he can add an Icon image to a ListView.


Right now nothing is happening when I try to test it, it compiles fine, but no icons show up.

private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            OpenFileDialog shortcut = new OpenFileDialog();
            shortcut.Title = "Add Shortcut";
            shortcut.InitialDirectory = "C:\\";

            ImageList image = new ImageList();
            image.ImageSize = new Size(16, 16);

            if (radioButton1.Checked == true)
            {               
                shortcut.ShowDialog();
                Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(shortcut.FileName);
                image.Images.Add(icon);

                DesktopItemsLV.SmallImageList = image;

                radioButton1.Checked = false;
            }
            else
            {
                return;
            }

        }

Any Ideas? Thanks

I would recommend you have the image list already on your form and associated with the LV.
Then just add the new image to the image list.

As you have it coded now you can only have one image in the list.
The next time an image is added the old image list is lost.

Also, to see the image against an item you need to set the items ImageIndex property.
E.G. DesktopItemsLV.Items[0].ImageIndex = 0;

I have tried that exact line:

DesktopItemsLV.Items[0].ImageIndex = 0;

But when I put that it spits out a runtime error saying value of 0 is not valid for 'Index'

Did you set the index before or after assigning the imagelist? Try setting it up the way nick.crane suggested; Add an empty imagelist to the form in the designer and tie it to the ListView.

If you want to avoid an exception when trying to assign the index you can check the index is in range before applying it:

int index = 0;
    if (listView.SmallImageList.Images.Count > index)
        listView.Items[0].ImageIndex = index;
This article has been dead for over six months. Start a new discussion instead.