Results 1 to 13 of 13

Thread: Compare two listviews

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Question Compare two listviews

    Here's the problem, I have two listviews. ListView1 has the items that needs to be checked (a pre-loaded list of items). When checking the items, all items from actual physical inventory are thrown to ListView2. Now, the program must compare all items that were found from checking or those that are placed on ListView2 with ListView1 and all items that are on listview2 will be deleted from ListView1 , so the items that should be left on listview1 should be the ones considered as missing.

    Listview1
    Item1
    item2
    item3
    item4
    item5

    ListView2(item found)
    item5
    item1
    item3

    ListView1 content after comparing
    item2
    item4

    If you have any other means like highlighting the items that are missing instead of deleting them from listview1, I will really appreciate it

    Thank you so much!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare two listviews

    Loop through the Items of ListView2 and get the Text of each one. Call FindItemWithText on ListView1 to find a matching item if there is one. If you find a match, Remove it from ListView1.

    Note that you will have to call FindItemWithText inside a Do or While loop because it finds partial matches. If you do find a match then you will have to compare the Text of that found item with the Text of the original item to see if they are equal. If not then you will have to look again, starting at the index of the next item.

    Before you ask for "an example", I'm more than happy to help further but only after you make an attempt for yourself. You don't know that you can't do it if you don't try and a lot of people don't even bother. As I said, I'm more than willing to help but I'm not here so that you don't have to try. If you were going to try anyway then I apologise but I've seen far too many cases where no attempt was made in similar circumstances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Compare two listviews

    jmcilhinney , I am now trying your suggestions and I will get back as soon as I finish it. Thank you for your help and for the challenge

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare two listviews

    Quote Originally Posted by JohnCloud View Post
    jmcilhinney , I am now trying your suggestions
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Compare two listviews

    I Will be using the first column of listview to compare the items, that would be an itemcode which is unique. While looping on listview2, should I throw the itemcodes from listview2 on a textbox and on textbox change remove that itemcode from listview1? is this the same with FindItemWithText?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Compare two listviews

    jmcilhinney, I might really have to this the other way, FindItemWithText is not supported on windowsce develpment or windows mobile.. yeah, im developing this on a mobile and should have posted this on mobile development thread, but there are only few who visits that thread and rarely gets any response. So What i have been doing is try it on windows application as simple as possible then try to work it on mobile, so I use only the basic of codes.

    But this topic is still for desktop. Just need it to be simple as possible.. I apologize for this.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Compare two listviews

    having said that I must use basic coding here, tell me if this makes sense or you have any better idea...

    I will loop through listview2
    Dim lvi As ListViewItem
    For Each lvi In ListView2.Items
    TextBox1.Text = lvi.Text
    Next

    then from textbox1 change
    "Delete FROM table001 WHERE ItemCode='" & texbox1.Text & "

    then reload listview1 contents which is connected to the database.

    you think the logic makes sense?
    thank you!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare two listviews

    In that case, you can simply use nested loops. Use a For Each loop to go through the Items in ListView2. Get the Text of the current item and then use a For loop to go through the Items in ListView1. Inside the inner loop, compare the Text of the current item from ListView1 and, if it matches, Remove the item and exit the loop. That's basically what using FindItemWithText does anyway but it hides the inner loop inside the method call.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Compare two listviews

    try this
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For j As Integer = ListView1.Items.Count - 1 To 0 Step -1
                For k As Integer = 0 To ListView2.Items.Count - 1
                    If ListView1.Items(j).Text.ToLower = ListView2.Items(k).Text.ToLower Then
                        'ListView1.Items.RemoveAt(j) ' Uncomment to delete insted of highlighting.
                        ListView1.Items(j).BackColor = SystemColors.Highlight
                        ListView1.Items(j).ForeColor = SystemColors.HighlightText
                    End If
                Next
            Next
        End Sub



  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Compare two listviews

    Thank you to both of you!

    4x2y, thanks to your code, i will see how I can make use of that on my project and its good for future reference. Thanks!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare two listviews

    Quote Originally Posted by 4x2y View Post
    try this
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For j As Integer = ListView1.Items.Count - 1 To 0 Step -1
                For k As Integer = 0 To ListView2.Items.Count - 1
                    If ListView1.Items(j).Text.ToLower = ListView2.Items(k).Text.ToLower Then
                        'ListView1.Items.RemoveAt(j) ' Uncomment to delete insted of highlighting.
                        ListView1.Items(j).BackColor = SystemColors.Highlight
                        ListView1.Items(j).ForeColor = SystemColors.HighlightText
                    End If
                Next
            Next
        End Sub
    There are some significant inefficiencies and other issues with that code so here's how I would suggest doing it, as we're posting code:
    vb.net Code:
    1. Dim items1 = Me.ListView1.Items
    2.  
    3. For Each item2 As ListViewItem In Me.ListView2.Items
    4.     Dim item2Text = item2.Text
    5.  
    6.     For i = 0 To items1.Count - 1
    7.         Dim item1Text = items1(i).Text
    8.  
    9.         If item1Text.Equals(item2Text, StringComparison.CurrentCultureIgnoreCase) Then
    10.             items1.RemoveAt(i)
    11.  
    12.             Exit For
    13.         End If
    14.     Next
    15. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Compare two listviews

    Quote Originally Posted by jmcilhinney View Post
    There are some significant inefficiencies and other issues with that code so here's how I would suggest doing it, as we're posting code:
    vb.net Code:
    1. Dim items1 = Me.ListView1.Items
    2.  
    3. For Each item2 As ListViewItem In Me.ListView2.Items
    4.     Dim item2Text = item2.Text
    5.  
    6.     For i = 0 To items1.Count - 1
    7.         Dim item1Text = items1(i).Text
    8.  
    9.         If item1Text.Equals(item2Text, StringComparison.CurrentCultureIgnoreCase) Then
    10.             items1.RemoveAt(i)
    11.  
    12.             Exit For
    13.         End If
    14.     Next
    15. Next
    Two notes
    1- Exit For prevents removing dublicated itemes in ListView1
    2- When modifying a collection inside loop, it must be reversed loop



  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compare two listviews

    Quote Originally Posted by 4x2y View Post
    1- Exit For prevents removing dublicated itemes in ListView1
    There are no duplicate items in this case, as stated in post #5.
    Quote Originally Posted by 4x2y View Post
    2- When modifying a collection inside loop, it must be reversed loop
    That's only so that the indexes of the items you are yet to visit are not affected but because the fact that there are no duplicate items means that you can, and SHOULD, break out of the loop as soon as you find a match, it doesn't matter about changing indexes because we're not going to visit any more items anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width