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.