Solved

Compare Listboxes

Posted on 2010-08-23
8
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Expert Comment

by:magdy99
ID: 33507587
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 63

Expert Comment

by:Fernando Soto
ID: 33507595
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
ID: 33515765
I am using .Net 3.5 And Visual Studio 2008. I am assigning string data to the Listbox.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33516473
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
 

Author Comment

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

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33516840
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
ID: 33516908
Worked Perfectly
0
 
LVL 63

Expert Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …

726 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