-
Mar 7th, 2018, 07:33 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Listview items comparison
I have a problem with comparing items in a ListView.
Edit: Well, more precisely, I am having problems comparing properties held within the items .Tag properties... Or, sort of...
As you can see in the image, player A has his city presented in the ListView multiple times. What I would like to achive is that each and one of his cities are compared, and only the city with the highest points will appear in the list. The players with only one city is easier, I just add them to a "keep this" list and deal with them later.
However, each ListViewItem has a CityObject object attached to its .Tag property which again holds the properties needed for the comparisons (the ListViewItems are not compared directly):
These properties are used from CityObject:
- Id (= city id) (as Long)
- ServerId (as Integer)
- CreationTimeFromAppAsDate (as Date)
- GetPointsTotal (as Long)
* If two or more of his cities have the same score, the CreationTime property should be used (remove all but the newest), else remove cities based on the score.
So, doing comparisons like this sound "easy", right? But these things are some of the toughest parts for me to wrap my head around regarding programming, and I have spent literarely two nights now trying to get this to work.
I need an eureka moment; someone please provide me with some advice on how you would perform comparison tasks like this. Need insight on the logical part...
Last edited by Arve K.; Mar 7th, 2018 at 09:44 PM.
-
Mar 7th, 2018, 07:38 PM
#2
Thread Starter
Fanatic Member
Re: Listview items comparison
I have written and then re-written the method to do the job so many times that I have lost count, but here is my latest try. It is far from done, but I can't concentrate anymore on this tonight, so I'll just post it here so that I can get some fresh eyes on it. Is 'code blind' a term? If not, I am copyrighting it
I am using a control named BetterListView, it behaves just like the standard ListView, except it got some additional features, but none of them are used here...
vb.net Code:
Public Shared Sub PerformFiltering(cityListView As BetterListView) Dim CityItems As BetterListViewItemCollection = cityListView.Items Dim City As GameData.CityObject = Nothing Dim ItemsToBeReAddedToTheCollection As New List(Of BetterListViewItem) Dim CityDataCollection As New List(Of CityData) For Each Item As BetterListViewItem In CityItems City = DirectCast(Item.Tag, GameData.CityObject) Dim NewData As New CityData With { .CityId = City.Id, .ServerId = City.ServerId, .TimeAdded = City.CreationTimeFromAppAsDate, .TotalScorePoints = City.GetPointsTotal } If GetCurrentCityInCollectionCount(City, CityItems) = 1 Then ItemsToBeReAddedToTheCollection.Add(Item) Else If Not CityDataCollection.Count = 0 Then For Each CityDataItem As CityData In CityDataCollection If CityDataItem.CityId = City.Id AndAlso CityDataItem.ServerId = City.ServerId Then If CityDataItem.TotalScorePoints = City.GetPointsTotal Then If CityDataItem.TimeAdded <= City.CreationTimeFromAppAsDate Then ItemsToBeReAddedToTheCollection.Add(Item) End If End If Else CityDataCollection.Add(NewData) End If Next Else CityDataCollection.Add(NewData) End If End If Next cityListView.Items.Clear() cityListView.Items.AddRange(ItemsToBeReAddedToTheCollection) End Sub
vb.net Code:
Friend Shared Function GetCurrentCityInCollectionCount(city As GameData.CityObject, collection As BetterListViewItemCollection) As Integer Dim i As Integer = Nothing For Each item As BetterListViewItem In collection If DirectCast(item.Tag, GameData.CityObject).Id = city.Id AndAlso DirectCast(item.Tag, GameData.CityObject).ServerId = city.ServerId Then i += 1 End If Next Return i End Function
vb.net Code:
Friend Class CityData Friend CityId As Long = Nothing Friend ServerId As Integer = Nothing Friend TimeAdded As Date = Nothing Friend TotalScorePoints As Long = Nothing End Class
Last edited by Arve K.; Mar 7th, 2018 at 09:39 PM.
-
Mar 7th, 2018, 10:14 PM
#3
Re: Listview items comparison
Here's an example of grouping, sorting, then selecting the first item in the group, which, by your specifications, is the item you're looking for...
WindowsApplication1.zip
BTW, that's a copywritten solution, and I will be collecting royalties
Edit: The date field should probably be sorted descending, but that should be an easy modification...
Last edited by .paul.; Mar 7th, 2018 at 10:49 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 8th, 2018, 07:31 AM
#4
Thread Starter
Fanatic Member
Re: Listview items comparison
That did it .paul., and it was indeed a neat way to solve it.
I would never have thought of using a query like that... Instead it feels like I am always trying to re-invent the wheel. All these powerful tools are right there in front of me, but somehow I never see them...
For the royalty fee, I have transferred one million dollars
Last edited by Arve K.; Mar 8th, 2018 at 08:19 AM.
-
Mar 8th, 2018, 08:51 AM
#5
Re: [RESOLVED] Listview items comparison
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|