Solved

Listbox duplicate Items delete

Posted on 2007-03-28
12
357 Views
Last Modified: 2010-04-16
Hello Experts,

I have two Listboxes ListBox1 and ListBox2. How do I delete items in ListBox1 if the same Items are in ListBox2.

For eg: If my listbox1 contains
NY
NJ
CA
GA
PA
and My Listbox 2 contains

NJ
PA
CA
RA
KA
FA
PA

How do I delete  the following items from my listbox1.
NJ
CA
PA


This is just a generic example I gave what I actually want is, I just want to delete Items from ListBox1 that are already in Listbox 2.

Thanks in advance
0
Comment
Question by:rowmark
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 18808827
Here is some code to remove dupes for 1 list box, easy to modify it for two:

bool found = false;
                    foreach (object o in listBox.Items)
                    {
                        if (o.ToString() == "Correct Entry")
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        listBox.Items.Insert(0, "Correct entry");
                   
                    }

Basically thats saying for each item in the listbox (for example Correct entry), check to see if there is already an identical item there.  If there is then stop but if there isnt (!found being false; not found) add it to the list.  
0
 
LVL 12

Expert Comment

by:craskin
ID: 18808853
First create a boolean function that takes the listbox and a listitem as variables and simply loops through the listbox and returns true if any of the items in the listbox match the listitem.

Function IsInListBox(lb as ListBox, li as ListItem) As Boolean
  Dim boolReturn as Boolean = False  
  For each ili as ListItem in lb.Items
       If ili.Value = li.Value Then
            boolReturn = True
       End If
  Next
  Return boolReturn
End Function

then loop through the other listbox and do something like

For each ili as ListItem in ListBox1.Items
    If IsInListBox(ListBox2, ili) = True Then
         ili.Remove  'could be ili.Delete
    End If
Next
       
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18808875
I suppose that your "master" listbox is Listbox 2.
- Fill the map with items from listbox2.
- scan the items in listbox1. If you have an item in map then delete it from listbox1. Better way - not insert it at all.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:empoalp
ID: 18809014
           foreach (Object obj in listBox2.Items)
            {
                if (listBox1.Items.Contains(obj))
                {
                    listBox1.Items.Remove(obj);
                }
            }
0
 

Author Comment

by:rowmark
ID: 18809034
ListBox1 is the Master Listbox. Currently I am doing as shown below

if (ListBox2.Items.Count > 0)
            {
                for (int i = ListBox2.Items.Count-1; i >= 0; i--)
                {
                    if (ListBox2 .Items[i].Selected)
                    {
                        ListBox1.Items.Remove(ListBox1.Items[i]);
                    }
                }

            }    


Instead of if (ListBox2 .Items[i].Selected) How do make it such that all the items in Listbox2 will be removed from listbox1?

Please help.


0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18809040
0
 
LVL 2

Expert Comment

by:empoalp
ID: 18809059
Try my solution. It works.
0
 
LVL 2

Accepted Solution

by:
empoalp earned 500 total points
ID: 18809169
And if ListBox2 have a lot more items than ListBox1 this could be more efficient:
           foreach (Object obj in ListBox1.Items)
            {
                if (ListBox2.Items.Contains(obj))
                {
                    ListBox1.Items.Remove(obj);
                }
            }
0
 

Author Comment

by:rowmark
ID: 18809195
This is what I am trying empoalp.

         foreach (Object obj in ListBox2.Items)
            {
                if (ListBox1.Items.Contains(obj))
                {
                    ListBox2.Items.Remove(obj);
                }
            }


I am getting the following errors:

Error      38      The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Contains(System.Web.UI.WebControls.ListItem)' has some invalid arguments      

Error      39      Argument '1': cannot convert from 'object' to 'System.Web.UI.WebControls.ListItem'
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18809266
Hi empoalp,
yes, it works but speed depends from list box items count. It is about M*N.
In addition, it is not a good way to remove the wrong items.
In my opinion the best way is not to add the wrong items.
0
 

Author Comment

by:rowmark
ID: 18809365
Any Idea why it throws errors for me?
0
 
LVL 2

Expert Comment

by:empoalp
ID: 18809705
Ok. The solution i have put is for Windows.Forms not for ASP.
Maybe (but not sure) this will work:
        foreach (ListItem item in ListBox1.Items)
        {
            if (ListBox2.Items.Contains(item))
            {
                ListBox1.Items.Remove(item);
            }
        }
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

808 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