Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

C#

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



VB

VB
(From item In listBox2.Items Where Not listBox1.Items.Contains(item)item).ToList().ForEach(Function(x) listBox2.Items.Remove(x))
 
Share this answer
 
v4
try this


C#
for (int i = 0; i <listBox2.Items.Count ; i++)
         {
             object item = listBox2.Items[i];
             if (!listBox1.Items.Contains(item))
             {
                 listBox2.Items.Remove(item);
                 i--;
             }
         }
 
Share this answer
 
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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900