Click here to Skip to main content
12,359,684 members (70,812 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
How we can remove the listbox items which are not exists in another listbos?

I have given an example here....

ListBox1 items are....

One
Two
Three
Four

ListBox2 items are....

One
Two
Three
Four
Five
Six

Please note that, items "Five" and "Six" in ListBox2 are not exists in ListBox1. So, these two should be deleted.

So, please guide me in this regard.
Ashu
Posted 25-Nov-12 18:44pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You could also do this if you prefer a less imperative approach.

C#

(from object item in listBox2.Items
 where !listBox1.Items.Contains(item)
 select item).ToList().ForEach(x=>listBox2.Items.Remove(x));


VB

(From item In listBox2.Items Where Not listBox1.Items.Contains(item)item).ToList().ForEach(Function(x) listBox2.Items.Remove(x))
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try this


for (int i = 0; i <listBox2.Items.Count ; i++)
         {
             object item = listBox2.Items[i];
             if (!listBox1.Items.Contains(item))
             {
                 listBox2.Items.Remove(item);
                 i--;
             }
         }
  Permalink  
v3
Comments
T.D.V.Anil 10-Dec-12 5:25am
   
How we can remove the listbox items which already in another listbox?

I have given an example here....

ListBox1 items are....

One
Two
Three
Four

ListBox2 items are....

One
Two


Please note that, items "Five" and "Two" in ListBox2 are alresy exist in ListBox1. So, these two in listbox1 should be deleted.

So, please guide me in this regard.
Anil.

can you people plz help me in this,if any answers plzz mail it to tdv.anil4u@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100