1

I don't know what I'm doing wrong, but my code is quite similar to this one and I cannot make it to work. The images do not show, no matter what.

var listView = new ListView();
var imageList = new ImageList();
imageList.Images.Clear();
listView.Items.Clear();

var imagenes = Directory.GetFiles(csGlobal.rutaImagenes, "*.jpg", SearchOption.TopDirectoryOnly).OrderBy(f => f);
imageList.ImageSize = new Size(32, 32);

foreach (var image in imagenes)
{
    Bitmap img = new Bitmap(image);

    imageList.Images.Add(img);
}

listView.View = View.LargeIcon;
listView.LargeImageList = imageList;

for (int j = 0; j < imageList.Images.Count; j++)
{
    ListViewItem item = new ListViewItem();
    item.ImageIndex = j;
    listView.Items.Add(item);
}

edited fixed setting OwnerDraw to false (01/03/2017)

Community
  • 1
  • 1
jamiii
  • 93
  • 10
  • 1
    is the `View` property set to `LargeIcon` on the list view? is the listview added to the controls collection of a visible form afterwards? – Cee McSharpface Feb 28 '17 at 15:54
  • How your listView is linked to real Control? From the example it looks like local objects, not as control from some window. – Viliam Feb 28 '17 at 16:09
  • Meaning: Did you add the LV to your form? Does it show? Also: You should set the imagelistsize __before__ adding the images!! – TaW Feb 28 '17 at 16:22
  • @dlatikay Yes. The View property is set to LargeIcon on the list view. And yes, the listview is on the form in a visible way. – jamiii Mar 01 '17 at 19:17
  • @user2136076 well, I dragged the control to the form and in the listview I set the propery largeImageList the control: imageList. Don't know what you mean. – jamiii Mar 01 '17 at 19:18
  • @TaW yes I did. It appears white but not a single image shows. I just set the size before and still... :( – jamiii Mar 01 '17 at 19:19
  • is there any proof that the listview actually contains any items? for example, can you select and is a selection border or selection highlight visible? does it work if you add text to the items? – Cee McSharpface Mar 01 '17 at 19:24
  • Your code as is __works just fine here__. (Albeit the default Size is a bit small) How many files/images do you have in the `imagenes` collection?? Are you using the debugger, as you always should?? – TaW Mar 01 '17 at 19:29
  • I had OwnerDraw as true. Thanks guys! – jamiii Mar 01 '17 at 20:07

0 Answers0