|
-
Jan 14th, 2014, 08:52 PM
#1
Thread Starter
Lively Member
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!
-
Jan 14th, 2014, 09:01 PM
#2
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.
-
Jan 14th, 2014, 09:48 PM
#3
Thread Starter
Lively Member
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
-
Jan 14th, 2014, 09:55 PM
#4
Re: Compare two listviews
 Originally Posted by JohnCloud
jmcilhinney , I am now trying your suggestions
-
Jan 14th, 2014, 10:09 PM
#5
Thread Starter
Lively Member
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?
-
Jan 14th, 2014, 10:19 PM
#6
Thread Starter
Lively Member
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.
-
Jan 14th, 2014, 10:39 PM
#7
Thread Starter
Lively Member
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!
-
Jan 14th, 2014, 10:41 PM
#8
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.
-
Jan 14th, 2014, 10:57 PM
#9
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
-
Jan 15th, 2014, 12:41 AM
#10
Thread Starter
Lively Member
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!
-
Jan 15th, 2014, 12:51 AM
#11
Re: Compare two listviews
 Originally Posted by 4x2y
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:
Dim items1 = Me.ListView1.Items For Each item2 As ListViewItem In Me.ListView2.Items Dim item2Text = item2.Text For i = 0 To items1.Count - 1 Dim item1Text = items1(i).Text If item1Text.Equals(item2Text, StringComparison.CurrentCultureIgnoreCase) Then items1.RemoveAt(i) Exit For End If Next Next
-
Jan 15th, 2014, 01:12 AM
#12
Re: Compare two listviews
 Originally Posted by jmcilhinney
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:
Dim items1 = Me.ListView1.Items For Each item2 As ListViewItem In Me.ListView2.Items Dim item2Text = item2.Text For i = 0 To items1.Count - 1 Dim item1Text = items1(i).Text If item1Text.Equals(item2Text, StringComparison.CurrentCultureIgnoreCase) Then items1.RemoveAt(i) Exit For End If Next Next
Two notes
1- Exit For prevents removing dublicated itemes in ListView1
2- When modifying a collection inside loop, it must be reversed loop
-
Jan 15th, 2014, 01:41 AM
#13
Re: Compare two listviews
 Originally Posted by 4x2y
1- Exit For prevents removing dublicated itemes in ListView1
There are no duplicate items in this case, as stated in post #5.
 Originally Posted by 4x2y
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|