Solved

Listbox duplicate Items delete

Posted on 2007-03-28
12
355 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Editing XSL files, 2 21
Response.Redirect Open new Form/tab/page? 5 20
getting id from database 5 24
Problem to page 4 23
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now