Solved

Compare Two ListViews

Posted on 2011-09-24
34
488 Views
Last Modified: 2013-11-27
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
Comment
Question by:Jack_Jones
  • 17
  • 5
  • 5
  • +2
34 Comments
 
LVL 12

Expert Comment

by:Deepu Chowdary
ID: 36594349
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36594367
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
 
LVL 1

Expert Comment

by:rockas1982
ID: 36601406
    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
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602215
So for me it only checks 1 item for some reason?
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602219
could you paste the code ?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602242
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
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602285
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
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602331
stops on the first true value
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602358
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
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602418
I do have team viewer
0
 
LVL 1

Expert Comment

by:rockas1982
ID: 36602439
send it here [EMAIL ADDRESS REMOVED]
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36602448
Sent ;)
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36635087
Well toying around with it still, is it easier for it to find the false values?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36712144
No luck yet, anyone? Please lol!
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36714024
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36714533
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36714538
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
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36716054
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
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36716404
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36716487
Ack, can't believe I missed that. Thanks Shaun. That will fix the index problem (and the error).
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36793486
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
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36814370
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 36816615
I agree that the for each is easier since it's less likely to mess up.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36891912
Yeah im still working on this, but even if they are not equal it won't display the values...
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36892524
Can you provide the code as you have it now?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36896915
Shaun pretty sick, ill try to get that to you asap just wanted to let you know im just relaxing today.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36914414
Shaun, sorry for the delay! No error message is generated, there are items that are = but not added to the listview.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 36917225
Can you provide the code as you have it now?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36937268
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
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 36943073
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
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36944907
awesome will try it out, let ya know once it's done!
0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 36965649
Cheers~
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now