Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 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 at 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
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

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

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 279
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
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