-1

I am able to get this result successfully from the sample code here C# .net Windows Forms Listview with image in Detail View using .NET Framework however the same code and form setup using.NET6.0 does not work i.e. the image does not show in the listview. I tried many many suggestions until I realized that the .NET was causing my issues.

Am I missing something for .NET to show the images in the listview?

public partial class Form1 : Form
{
    List<string> _list = new();
    ImageList serviceLogoList = new ImageList
    {
        ImageSize = new Size(32, 32),
        ColorDepth = ColorDepth.Depth32Bit
    };


    public Form1()
    {
        InitializeComponent();
        initializeListView();


        DirectoryInfo dir = new DirectoryInfo(@"\resources"); //change and get your folder
        foreach (FileInfo file in dir.GetFiles())
        {
            try
            {
                this.serviceLogoList.Images.Add(Image.FromFile(file.FullName));
            }
            catch
            {
                //log error
            }
        }

        foreach (var str in _list)
        {
            ListViewItem item = new ListViewItem();
            item.Text = str;
            item.SubItems.Add("OK");
            item.ImageIndex = 0; //tried 1,2,3,4,5 as well
            listView1.Items.Add(item);
        }
    }
    private void initializeListView()
    {
        _list.Add("Test1");
        _list.Add("Test2");
        _list.Add("Test3");

        listView1.View = View.Details;
        //listView1.LargeImageList = serviceLogoList;
        listView1.SmallImageList = serviceLogoList;
        listView1.Columns.Add("Name", 100);
        listView1.Columns.Add("Status", 100);
    }
}

Here is the code as requested. This code works when .NET Framework 4.7.2 is selected instead of .NET 6.0 (LTS). This code is courtesy of a post on StackOverflow but I can't find the link at the moment. The code works based on "Windows Forms App (.NET Framework)" but not on "Windows Form App"

Add a listview control to the form called listView1. Everything else is done in the code behind.

.NET Framework enter image description here

jeranio
  • 53
  • 8
  • Could you provide some code and actually show us what does not work? Are you getting an error? Or they simply don't show in the Listview? We need a little more info, it is not quite clear what your issue is – Gabriel Stancu Nov 23 '22 at 09:01
  • View.LargeIcon requires the ListView.LargeImageList property to be set, setting SmallImageList produces items without images. In 4.7.2 as well. – Hans Passant Nov 23 '22 at 10:57
  • Sorry, I edited it since I am using "View.Details" , I have included the output from each type of project – jeranio Nov 23 '22 at 11:50

1 Answers1

0

An update on this. It seems the control I was using which was from the MaterialSkin (link takes you to an issue raised by someone else) package does not support images. I changed out the control for a Winform one and everything works as intended. I created a blank form, added one listview control named listview1, and changed the view type to details. I will style the control to match the rest of my UI.

    public Form1()
    {
        InitializeComponent();
        ImageList imageListSmall = new ImageList();
        imageListSmall.Images.Add(Bitmap.FromFile("D:\\subtract.png"));
        imageListSmall.Images.Add(Bitmap.FromFile("D:\\add.png"));
        listView1.SmallImageList = imageListSmall;

        listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);

        listView1.Items.Clear();

        ListViewItem item1 = new ListViewItem("item1", 0);
        listView1.Items.Add(item1);
        item1 = new ListViewItem("item2", 1);
        listView1.Items.Add(item1);
       

    }
jeranio
  • 53
  • 8