Solved

Compare Listboxes

Posted on 2010-08-23
8
448 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

688 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