Solved

Listbox duplicate Items delete

Posted on 2007-03-28
12
358 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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