4 Replies - 334 Views - Last Post: 05 November 2019 - 01:39 AM Rate Topic: -----

#1 Danamo51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-November 19

ListView Images get lost/scrambled when add a new item

Posted 04 November 2019 - 12:21 AM

I have a listview linked to an imagelist. When the program starts it loads images from files into the image list
and then adds them into the listview along with the name of the item in the picture.

This all works fine when the program first starts, but the user can add new images into the imagelist and corresponding new items with those images into the listview, and the listview needs to be sorted to put the new items in alpha order. At this point the listview gets messed up. Either no picture appears with some of the items in the listview and/or the wrong pictures do.

How do I keep the imagelist indexes linked to a listview after the listview gets sorted? I've tried using an ID number as a tag to the listview item where the ID is the imagelist index number (the ID is also used for other purposes in my program.) I've also tried setting the ID as the listview.imagekey, and/or imageindex, but that doesn't work either.

The sorting method for the list view is a ListViewColumnSorter class using a System.Collections.IComparer that I've found in multiple websites, although I initially tried just setting the Sorting property of the listview in the Designer. The results are the same either way. Scrambled listviews.

If I close the program and then restart it, though, everything is fine. The listview appears as it should, with the listview items and images appearing properly. (New items added by the user are appended to a file which the program uses to reference the images and other data to be loaded when the program first starts.)

Obviously I am lacking in my knowledge about listviews and image collections! What the heck am I doing wrong? Could someone please point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: ListView Images get lost/scrambled when add a new item

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: ListView Images get lost/scrambled when add a new item

Posted 04 November 2019 - 03:20 AM

Please provide some code, wrapped in code tags.

If there are errors provide the error details.
Was This Post Helpful? 0
  • +
  • -

#3 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: ListView Images get lost/scrambled when add a new item

Posted 04 November 2019 - 05:21 AM

Why not use the image Key rather than Image Index?

Removed my unhelpful comment.

I still stand by it, I believe you should be using the image key rather than syncing up the the Index's which will never work.

IF you think about it this way, your data should look a little like this:

Item {ID:=1, RowIndex:=1} - Image 1 {Index:=1, ImageKey:= ItemID1}
Item {ID:=2, RowIndex:=2} - Image 2 {Index:=2, ImageKey:= ItemID2}
Item {ID:=3, RowIndex:=3} - Image 3 {Index:=3, ImageKey:= ItemID3}

Where Id is your internal Id, either the line number it read from a text file, or a unique field somehow determined by the data on each row. As you stated, when the load the data, there is a link between RowIndex and Index in the Image List.

But as soon as you order your grid, the index's are out of scope.

However, as your adding in images to the image list, if you provider a image key that links back to the unique ID, your golden, you know what image you should be displaying at all times.

Item {ID:=1, RowIndex:=3} - Image 1 {Index:=1, ImageKey:= ItemID1}
Item {ID:=2, RowIndex:=2} - Image 2 {Index:=2, ImageKey:= ItemID2}
Item {ID:=3, RowIndex:=1} - Image 3 {Index:=2, ImageKey:= ItemID3}

This post has been edited by maceysoftware: 04 November 2019 - 05:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 Danamo51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-November 19

Re: ListView Images get lost/scrambled when add a new item

Posted 04 November 2019 - 11:18 AM

View Postmaceysoftware, on 04 November 2019 - 05:21 AM, said:

Why not use the image Key rather than Image Index?

I still stand by it, I believe you should be using the image key rather than syncing up the the Index's which will never work.

Thank you very much. Your comment about referencing the image and list keys instead of the indexes was the clue I needed to solve the problem. Due to my ignorance about how listviews work in conjunction with imagelists I was trying to reference the imagelist indexes instead of using the keys properly, so then re-sorting the listview after adding a new image and item caused the listview to say WTF? and get confused. When the keys in both match each other then there's no problem and all is well now.

I'd buy you lunch or a drink for your help but the dreamincode forum doesn't seem to have a bar or restaurant! So, Thanks again for your help.
Was This Post Helpful? 0
  • +
  • -

#5 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,640
  • Joined: 07-September 13

Re: ListView Images get lost/scrambled when add a new item

Posted 05 November 2019 - 01:39 AM

Your welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1