When I add items to a ListView in any mode except LargeIcon , the ListView stops showing images with a LargeImageList when it switches to LargeIcon . This situation persists until a new item is added to the ListView in LargeIcon mode.
Thus, the following sequence illustrates the problem:
- create
ListView , add column, set View to Details - create an
ImageList , set ImageSize , assign it to ListView.LargeImageList - create a new
ListViewItem , set it to ImageKey - create a new image, add it to
ImageList with the given key - add
ListViewItem to ListView - switch
ListView mode to LargeIcon - repeat steps # 3 - # 6, now in
LargeIcon mode- all images are displayed as expected
What am I still missing?
I tried the following:
- Cancel
ListView LargeImageList before / after adding item (even via null )
Test code for those who like it more than words:
public partial class Form1 : Form { int counter = 0; ImageList iList = new ImageList(); private string GetNewKey() { return counter++.ToString(); } private Image GetNewImage(Size size) { var bmp = new Bitmap(size.Width, size.Height); using (var gra = Graphics.FromImage(bmp)) { var rnd = new Random(); var lines = rnd.Next(1000); for (int l = 0; l < lines; ++l) { var pen = new Pen(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))); var p1 = new Point(rnd.Next(size.Width), rnd.Next(size.Height)); var p2 = new Point(rnd.Next(size.Width), rnd.Next(size.Height)); gra.DrawLine(pen, p1, p2); } } return bmp; } public Form1() { InitializeComponent(); iList.ImageSize = new Size(100, 100); listView.LargeImageList = iList; listView.Columns.Add("name"); } private void buttonAdd_Click(object sender, EventArgs e) { var key = GetNewKey(); var lvi = new ListViewItem() { Name = key, Text = "blabla", ImageKey = key, }; iList.Images.Add(key, GetNewImage(new Size(100, 100))); listView.Items.Add(lvi); } private void buttonClear_Click(object sender, EventArgs e) { listView.Items.Clear(); } private void buttonLarge_Click(object sender, EventArgs e) { listView.View = View.LargeIcon; } private void buttonDetails_Click(object sender, EventArgs e) { listView.View = View.Details; } }
EDIT:
For those who will experience the same problem. After some experiments, there is at least a stupid inconvenience for a person:
Change ImageList , ListView somehow detects its change and reloads images for LargeIcon mode. Questions: how it detects the change and why it ignores the ImageList change after the mode change ...
private void FixIt() {
EDIT # 2: I also discovered some other funny features that ListView and related components have. You can check them in the answers to the question question 4097912 and question 23059678
source share