Solved

Compare Listboxes

Posted on 2010-08-23
8
429 Views
Last Modified: 2012-05-10
I have to listboxes. I need to check them for differences. How would I check them and if an item is new or different, display that item?
0
Comment
Question by:XGenwareS
  • 4
  • 3
8 Comments
 

Expert Comment

by:magdy99
Comment Utility
use another listbox listbox3 for example and button  and put the following code

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim count As Integer
        count = ListBox2.Items.Count


        For i As Integer = 0 To count - 1
            Dim item As Object = ListBox1.Items.Item(i)
            If Not ListBox2.Items.Contains(item) Then
                ListBox3.Items.Add(item)
            End If


        Next
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi XGenwareS;

More info would be helpful. What version of .Net Framework and Visual Studio? What is being assigned to the ListBox, String data or Class Object?

Fernando
0
 

Author Comment

by:XGenwareS
Comment Utility
I am using .Net 3.5 And Visual Studio 2008. I am assigning string data to the Listbox.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi XGenwareS;

Because you are using the .Net Framework 3.5 you can use Linq to solve your issue. The list of strings in lbDiff in the code snippet will contain the difference between the two list boxes.

Fernando
Dim lb1 As List(Of String) = ListBox1.Items.Cast(Of Object)().Select(Function(s) s.ToString()).ToList()

Dim lb2 As List(Of String) = ListBox2.Items.Cast(Of Object)().Select(Function(s) s.ToString()).ToList()



Dim lbDiff As List(Of String) = lb1.Except(lb2).ToList()

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:XGenwareS
Comment Utility
Alright that appears to work. Now how would I display the items in lbDiff in say a messagebox?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi XGenwareS;

OK, try this.

Fernando
Imports System.Text



Dim lb1 As List(Of String) = ListBox1.Items.Cast(Of Object)().Select(Function(s) s.ToString()).ToList()

Dim lb2 As List(Of String) = ListBox2.Items.Cast(Of Object)().Select(Function(s) s.ToString()).ToList()



Dim lbDiff As List(Of String) = lb1.Except(lb2).ToList()



Dim stringDiff As New StringBuilder()

lbDiff.ForEach(Function(s) stringDiff.Append(s & vbCrLf))



MessageBox.Show(stringDiff.ToString())

Open in new window

0
 

Author Closing Comment

by:XGenwareS
Comment Utility
Worked Perfectly
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Very good, glad I was able to help.  ;=)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

18 Experts available now in Live!

Get 1:1 Help Now