Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

Compare Two ListViews

Greetings,

 I use Visual Studio 2010, Visual Basic Project. Is it possible to compare two listview colums and only export the onces that are true. I will be exporting to excel and I know how to export it, just not how to get the values that are a match. Please help with providing code!

Thanks~
0
Jack_Jones
Asked:
Jack_Jones
  • 17
  • 5
  • 5
  • +2
1 Solution
 
Deepu ChowdaryCommented:
0
 
Jack_JonesAuthor Commented:
I know how to do the sort colum, looking more at how to extract a value if it's a match in listview1 and listview2. Like a nice loop to check if true then export it.
0
 
rockas1982Commented:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Exp As String
        For A = 0 To ListView1.Items.Count - 1
            For B = 0 To ListView1.Items.Count - 1
                If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                    Exp = Exp & ListView1.Items(A).Text
                End If
            Next
        Next
        MsgBox(Exp)
    End Sub

print screen
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
Jack_JonesAuthor Commented:
So for me it only checks 1 item for some reason?
0
 
rockas1982Commented:
could you paste the code ?
0
 
Jack_JonesAuthor Commented:
I also get,

 Variable 'Exp' is used before it has been assigned a value. A null reference exception could result at runtim.

Dim Exp As String
        Try
            For A = 0 To ListView1.Items.Count - 1
                For B = 0 To ListView1.Items.Count - 1
                    If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                        Exp = Exp & ListView1.Items(A).Text
                    End If
                Next
            Next
            ListView3.Items.Add(Exp)
        Catch
        End Try

Open in new window

0
 
rockas1982Commented:
try this

Dim Exp As String
        Try
            For A = 0 To ListView1.Items.Count - 1
                For B = 0 To ListView1.Items.Count - 1
                    If ListView1.Items(A).Text = ListView2.Items(B).Text Then
                        ListView3.Items.Add(ListView1.Items(A).Text)
                    End If
                Next
            Next
        Catch
        End Try

Open in new window


p.s. the warning you get it's not important
0
 
Jack_JonesAuthor Commented:
stops on the first true value
0
 
rockas1982Commented:
i don't understand why .. do you have team viewer to show me what are you doing ? or send me the project if it can run on any pc
0
 
Jack_JonesAuthor Commented:
I do have team viewer
0
 
rockas1982Commented:
send it here [EMAIL ADDRESS REMOVED]
0
 
Jack_JonesAuthor Commented:
Sent ;)
0
 
Jack_JonesAuthor Commented:
Well toying around with it still, is it easier for it to find the false values?
0
 
Jack_JonesAuthor Commented:
No luck yet, anyone? Please lol!
0
 
Jack_JonesAuthor Commented:
Rockas1982 and I left off with this, and it don't generate anything.

Dim A As Integer
        Dim B As Integer

        Try
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count
                    If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
                        foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
                        Exit For
                    End If
                Next B
            Next A
        Catch

        End Try

Open in new window

0
 
TommySzalapskiCommented:
Line 8 should just be
foundskus.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)

You could also try removing line 9 just in case it's exiting both for loops.

Oh, and in the old version, to get rid of the warning, you should have just done Exp="" before starting the loops.
0
 
TommySzalapskiCommented:
Also, either remove the try-catch or put something in the catch. If it's crashing, you won't know and also won't be told why because you don't handle your exceptions.
0
 
Jack_JonesAuthor Commented:
Tommy,

 Might be getting somewhere.

InvalidArgument=Value of '83' is not valid for 'index'.
Parameter name: index

Is generated error from;

 If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
The index in the line is not correct:
If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

A is for indexing CompliantSkus
B is for indexing VehicleSkus.

Try:
If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
0
 
TommySzalapskiCommented:
Ack, can't believe I missed that. Thanks Shaun. That will fix the index problem (and the error).
0
 
Jack_JonesAuthor Commented:
Shaun,

InvalidArgument=Value of '147' is not valid for 'index'.
Parameter name: index

If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then 

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
The index runs 0 to Count - 1.

Change these two lines:
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count
TO:
            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count - 1
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count - 1

An alternative is to use the For Each construct:
For Each A as ListItem in CompliantSkus.Columns(0).ListView.Items
   For Each B as ListItem in VehicleSkus.Columns(0).ListView.Items

Doing this, you have a ListItem object which can be easier to follow in code instead of using the index (IMHO).
0
 
TommySzalapskiCommented:
I agree that the for each is easier since it's less likely to mess up.
0
 
Jack_JonesAuthor Commented:
Yeah im still working on this, but even if they are not equal it won't display the values...
0
 
Shaun KlineLead Software EngineerCommented:
Can you provide the code as you have it now?
0
 
Jack_JonesAuthor Commented:
Shaun pretty sick, ill try to get that to you asap just wanted to let you know im just relaxing today.
0
 
Jack_JonesAuthor Commented:
Shaun, sorry for the delay! No error message is generated, there are items that are = but not added to the listview.
0
 
Shaun KlineLead Software EngineerCommented:
Can you provide the code as you have it now?
0
 
Jack_JonesAuthor Commented:
Here ya go buddy.

Dim A As Integer
        Dim B As Integer

            For A = 0 To CompliantSkus.Columns(0).ListView.Items.Count - 1
                For B = 0 To VehicleSkus.Columns(0).ListView.Items.Count - 1
                    If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
                        foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
                        Exit For
                    End If
                Next B
            Next A

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
This line is the culprit:
   If VehicleSkus.Columns(0).ListView.Items(A).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then
You are using the same index for both lists. This would only find items at the same position in both lists that are the same.

Try this:
   If VehicleSkus.Columns(0).ListView.Items(B).ToString = CompliantSkus.Columns(0).ListView.Items(A).ToString Then

Also, this line may cause you issues as you are using the index from the FOR loop for CompliantSkus
when adding items to foundskus:
   foundskus.Items(A).SubItems.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)

You may be able to do this:
   foundskus.Items.Add(VehicleSkus.Columns(0).ListView.Items(B).Text)
0
 
Jack_JonesAuthor Commented:
awesome will try it out, let ya know once it's done!
0
 
Jack_JonesAuthor Commented:
Cheers~
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 17
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now