[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Compare Listboxes

Posted on 2010-08-23
8
Medium Priority
?
461 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 64

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Make the most of your online learning experience.
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 …
Simple Linear Regression

649 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