Skip to content

Commit

Permalink
Fix ListView no longer displays images (#4184)
Browse files Browse the repository at this point in the history
  • Loading branch information
RussKie authored Nov 12, 2020
1 parent dac8607 commit d0608e7
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public sealed class ImageCollection : IList
/// issues by holding on to extra references.
private int _lastAccessedIndex = -1;

// Indicates whether images are added in a batch.
private bool _isBatchAdd;

/// <summary>
/// Returns the keys in the image list - images without keys return String.Empty.
/// </summary>
Expand Down Expand Up @@ -182,6 +185,8 @@ public Image this[int index]
bitmap.Dispose();
}
}

_owner.OnRecreateHandle(EventArgs.Empty);
}
}

Expand All @@ -195,7 +200,7 @@ object IList.this[int index]
throw new ArgumentException(SR.ImageListBadImage, nameof(value));
}

this[index] = (Image)value;
this[index] = image;
}
}

Expand Down Expand Up @@ -372,9 +377,10 @@ private int Add(Original original, ImageInfo imageInfo)
_imageInfoCollection.Add(imageInfo);
}

if (!_owner._inAddRange)
if (!_isBatchAdd)
{
_owner.OnChangeHandle(EventArgs.Empty);
_owner.OnRecreateHandle(EventArgs.Empty);
}

return index;
Expand All @@ -387,14 +393,15 @@ public void AddRange(Image[] images)
throw new ArgumentNullException(nameof(images));
}

_owner._inAddRange = true;
_isBatchAdd = true;
foreach (Image image in images)
{
Add(image);
}

_owner._inAddRange = false;
_isBatchAdd = false;
_owner.OnChangeHandle(EventArgs.Empty);
_owner.OnRecreateHandle(EventArgs.Empty);
}

/// <summary>
Expand Down Expand Up @@ -445,6 +452,7 @@ public void Clear()
}

_owner.OnChangeHandle(EventArgs.Empty);
_owner.OnRecreateHandle(EventArgs.Empty);
}

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -554,7 +562,9 @@ void IList.Remove(object value)
if (value is Image image)
{
Remove(image);

_owner.OnChangeHandle(EventArgs.Empty);
_owner.OnRecreateHandle(EventArgs.Empty);
}
}

Expand All @@ -575,7 +585,9 @@ public void RemoveAt(int index)
if ((_imageInfoCollection != null) && (index >= 0 && index < _imageInfoCollection.Count))
{
_imageInfoCollection.RemoveAt(index);

_owner.OnChangeHandle(EventArgs.Empty);
_owner.OnRecreateHandle(EventArgs.Empty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public sealed partial class ImageList : Component, IHandle
private EventHandler _recreateHandler;
private EventHandler _changeHandler;

private bool _inAddRange;

/// <summary>
/// Creates a new ImageList Control with a default image size of 16x16
/// pixels
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WinformsControlsTest
Expand Down Expand Up @@ -223,5 +224,60 @@ private void listView2_SelectedIndexChanged(object sender, System.EventArgs e)
var random = new Random();
listView2.Columns[random.Next(0, listView2.Columns.Count)].ImageIndex = random.Next(0, 2);
}

private void btnClearListView1_Click(object sender, EventArgs e)
{
listView1.Clear();
LargeImageList.Images.Clear();

listView1.LargeImageList = LargeImageList;
listView1.View = View.LargeIcon;
}

private void btnLoadImagesListView1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}

foreach (string file in openFileDialog1.FileNames)
{
Bitmap bitmap = (Bitmap)Bitmap.FromFile(file);
LargeImageList.Images.Add(file, bitmap);

ListViewItem item = new ListViewItem
{
Text = Path.GetFileName(file),
Name = file,
ImageKey = file,
Checked = true
};
listView1.Items.Add(item);
}
}

private void btnReplaceImageListView1_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count != 1)
{
return;
}

openFileDialog1.Multiselect = false;
DialogResult result = openFileDialog1.ShowDialog();
openFileDialog1.Multiselect = true;

if (result != DialogResult.OK)
{
return;
}

string file = openFileDialog1.FileName;
Bitmap bitmap = (Bitmap)Bitmap.FromFile(file);
LargeImageList.Images[listView1.SelectedIndices[0]] = bitmap;

listView1.Refresh();
}
}
}

0 comments on commit d0608e7

Please sign in to comment.