dcsimg
Results 1 to 5 of 5

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    466

    Resolved [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...

    Name:  listview filtering.png
Views: 166
Size:  19.2 KB

    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 08:44 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  2. #2

    Thread Starter
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    466

    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:
    1. Public Shared Sub PerformFiltering(cityListView As BetterListView)
    2.  
    3.     Dim CityItems As BetterListViewItemCollection = cityListView.Items
    4.     Dim City As GameData.CityObject = Nothing
    5.     Dim ItemsToBeReAddedToTheCollection As New List(Of BetterListViewItem)
    6.     Dim CityDataCollection As New List(Of CityData)
    7.  
    8.     For Each Item As BetterListViewItem In CityItems
    9.         City = DirectCast(Item.Tag, GameData.CityObject)
    10.  
    11.         Dim NewData As New CityData With {
    12.             .CityId = City.Id,
    13.             .ServerId = City.ServerId,
    14.             .TimeAdded = City.CreationTimeFromAppAsDate,
    15.             .TotalScorePoints = City.GetPointsTotal
    16.         }
    17.  
    18.         If GetCurrentCityInCollectionCount(City, CityItems) = 1 Then
    19.  
    20.             ItemsToBeReAddedToTheCollection.Add(Item)
    21.        
    22.         Else
    23.             If Not CityDataCollection.Count = 0 Then
    24.                 For Each CityDataItem As CityData In CityDataCollection
    25.  
    26.                     If CityDataItem.CityId = City.Id AndAlso CityDataItem.ServerId = City.ServerId Then
    27.  
    28.                         If CityDataItem.TotalScorePoints = City.GetPointsTotal Then
    29.  
    30.                             If CityDataItem.TimeAdded <= City.CreationTimeFromAppAsDate Then
    31.  
    32.                                 ItemsToBeReAddedToTheCollection.Add(Item)
    33.                             End If
    34.  
    35.                         End If
    36.                     Else
    37.                         CityDataCollection.Add(NewData)
    38.  
    39.                     End If
    40.                 Next
    41.  
    42.             Else
    43.                 CityDataCollection.Add(NewData)
    44.  
    45.             End If
    46.         End If
    47.     Next
    48.  
    49.     cityListView.Items.Clear()
    50.     cityListView.Items.AddRange(ItemsToBeReAddedToTheCollection)
    51.  
    52. End Sub

    vb.net Code:
    1. Friend Shared Function GetCurrentCityInCollectionCount(city As GameData.CityObject, collection As BetterListViewItemCollection) As Integer
    2.     Dim i As Integer = Nothing
    3.  
    4.     For Each item As BetterListViewItem In collection
    5.         If DirectCast(item.Tag, GameData.CityObject).Id = city.Id AndAlso
    6.            DirectCast(item.Tag, GameData.CityObject).ServerId = city.ServerId Then
    7.  
    8.             i += 1
    9.  
    10.         End If
    11.     Next
    12.  
    13.     Return i
    14. End Function

    vb.net Code:
    1. Friend Class CityData
    2.     Friend CityId As Long = Nothing
    3.     Friend ServerId As Integer = Nothing
    4.     Friend TimeAdded As Date = Nothing
    5.     Friend TotalScorePoints As Long = Nothing
    6. End Class
    Last edited by Arve K.; Mar 7th, 2018 at 08:39 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,696

    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 09:49 PM.

  4. #4

    Thread Starter
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    466

    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 07:19 AM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,696

    Re: [RESOLVED] Listview items comparison

    :d :d :d :d

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.