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.
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 :)
Re: Compare two listviews
Quote:
Originally Posted by
JohnCloud
jmcilhinney , I am now trying your suggestions
:thumb:
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?
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.
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!
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.
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
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!
Re: Compare two listviews
Quote:
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
Re: Compare two listviews
Quote:
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
Re: Compare two listviews
Quote:
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.
Quote:
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.