Solved

Listbox duplicate Items delete

Posted on 2007-03-28
12
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

738 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