Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:31:42+00:00 2026-05-15T02:31:42+00:00

I am writing an application to compare each item on listbox1 to all items

  • 0

I am writing an application to compare each item on listbox1 to all items on listbox2. If the item is found, then remove it from both lists. The goal is to only have the items that were not found remain on both lists.

The problem is, the application just hangs and I never get any results. I looked at my code several times and I cannot figure out what’s going on (programming noob I know…).

Can anybody help me with this?

Code Snippet:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim a As String
    Dim b As String
    Dim y As String

    For i As Integer = 0 To ListBox1.Items.Count - 1
        a = ListBox1.Items(i)
        y = 1
        Do While y = 1
            For x As Integer = 0 To ListBox2.Items.Count - 1
                b = ListBox2.Items(x)
                Dim res As Int16 = String.Compare(a, b)
                If res = 0 Then
                    y = 0
                    ListBox2.Items.Remove(i)
                    ListBox2.Items.Remove(x)
                ElseIf x = ListBox1.Items.Count Then
                    Exit Do
                End If
            Next
        Loop
    Next
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:31:43+00:00Added an answer on May 15, 2026 at 2:31 am

    if ListBox1.Items.Count is more that ListBox2.Items.Count – 1, X will never equal ListBox1.Items.Count, so the Exit Do will never run, and the code will just loop endlessly in the

    Do While y = 1
    

    Have you considered using Linq for example, for easier list management?

    edit: Additionally it’s wrong to delete an item from the list you are traversing with a for (it’s downright illegal to do that with a For Each) because each deletion will offset the loop counter.

    edit2: here’s a Linq snippet that accomplishes the task:

        Dim itemsFirst = (From item As String In ListBox1.Items Select item)
        Dim itemsSecond = (From item As String In ListBox2.Items Select item)
    
        Dim dupes = System.Linq.Enumerable.Intersect(itemsFirst, itemsSecond).ToList
        For Each item In dupes
            ListBox1.Items.Remove(item)
            ListBox2.Items.Remove(item)
        Next item
    

    what is does is basically extract the strings from both list (this is necessary because the ListBox.Items collection is a little weird)

    After that we run the intersect method and copy the results into a list. (the .ToList part)
    The copying is a required part because, otherwise dupes would just be a subset of the Items of the ListBox, and once again we would be trying to lift ourselves by pulling on our shoestrings.

    The last part is just a simple delete loop, that removes the items from the collection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application that takes a users input (from a UITextView) and
I am writing a helper for my application which writes out a menu item
I am writing an application (c# + wpf) where all modal style dialogs are
I'm currently writing a proxy application that reads from one socket and writes on
I'm writing an application that needs to work on both mysql and postgresql. I
I am writing an application that receives data with items and version numbers. The
I'm currently writing a simple app to compare two lists and return differences if
I'm writing an OCR application to read characters from a screenshot image. Currently, I'm
I am writing application in Linux using C, pthreads and sockets. This will be
I am writing application on ActionScript for Android using Adobe AIR with native extentions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.