Solved

Listbox duplicate Items delete

Posted on 2007-03-28
12
360 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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