Windows Forms Topics include: windows forms, controls, components and designers

Go Back  Xtreme .NET Talk > .NET > Windows Forms > Remove Duplicate items from a ComboBox How?


Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2006, 12:49 AM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default Remove Duplicate items from a ComboBox How?



Just as the subject line said...

How do I reomve the duplicate items from a combobox...

Example;

If I had 4 items such as

Apple
Apple
Pears
Pears

I want it to read after removing

Apple
Pears
Reply With Quote
  #2  
Old 07-25-2006, 10:26 AM
mjb3030 mjb3030 is offline
Regular
 
Join Date: Aug 2003
Posts: 76
mjb3030 is on a distinguished road
Default

Code:
For i As Int16 = 0 To Me.ComboBox1.Items.Count - 2
     For j As Int16 = Me.ComboBox1.Items.Count - 1 To i + 1 Step -1
          If Me.ComboBox1.Items(i).ToString = Me.ComboBox1.Items(j).ToString Then
               Me.ComboBox1.Items.RemoveAt(j)
          End If
     Next
Next
Reply With Quote
  #3  
Old 07-25-2006, 12:28 PM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default

This worked perfect....

But if you dont mind... might I ask you a question or 2 about this code?

Code:
For i As Int16 = 0 To cboCity.Items.Count - 2 ' Why -2 here and
            For j As Int16 = cboCity.Items.Count - 1 To i + 1 Step -1 ' -1 with all the other here????
                If cboCity.Items(i).ToString = cboCity.Items(j).ToString Then
                    cboCity.Items.RemoveAt(j)
                End If
            Next
        Next


You put the count on the first line to - 2
but on the 2nd line -1 To i + 1 Step -1

Why? and What is actuially going on there?

One more question if I might...
You started your FOr loop with

For i As int16 Why not For i As Integer ???

I ask because thats the first time I saw in16 instead of Integer...

Trust me this is not to knock your code cause it works perfectly, its just an attempt to understand more about vb.net

vbMarkO
Reply With Quote
  #4  
Old 07-25-2006, 03:10 PM
Denaes's Avatar
Denaes Denaes is offline
Senior Contributor
 
Join Date: Jun 2003
Posts: 956
Denaes is on a distinguished road
Default

Quote:
Originally Posted by vbMarkO
This worked perfect....

But if you dont mind... might I ask you a question or 2 about this code?

Code:
For i As Int16 = 0 To cboCity.Items.Count - 2 ' Why -2 here and
            For j As Int16 = cboCity.Items.Count - 1 To i + 1 Step -1 ' -1 with all the other here????
                If cboCity.Items(i).ToString = cboCity.Items(j).ToString Then
                    cboCity.Items.RemoveAt(j)
                End If
            Next
        Next


You put the count on the first line to - 2
but on the 2nd line -1 To i + 1 Step -1

Why? and What is actuially going on there?
The first line is a loop from the first element to the next to last element.

The second line is a decrementing loop from the last element to the first.

Because the second loop is counting down rather than up, you don't need to go all the way to the last item in the first loop. It's already been checked.

Just one of many ways to skin this apple.

Quote:
Originally Posted by vbMarkO
One more question if I might...
You started your FOr loop with

For i As int16 Why not For i As Integer ???

I ask because thats the first time I saw in16 instead of Integer...
Integer is a nice way of saying Int32. You could use Int32, but it would be overkill. Normally in .Net Integer is just fine because a lot of .Net apps aren't that performance intensive.

This is a habbit you'll have if you make video games or many C++ apps, trying to squeeze your value into the best fit for the absolute best performance. It's a good practice, but IMO, not anything that I'm going to lose sleep over.
Reply With Quote
  #5  
Old 07-25-2006, 07:05 PM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default

Thank You that makes sense now...

I appreciate the time you took to break it down. This definately resolved

vbMarkO
Reply With Quote
  #6  
Old 07-26-2006, 10:55 AM
Diesel's Avatar
Diesel Diesel is offline
Contributor
 
Join Date: Aug 2003
Posts: 662
Diesel is on a distinguished road
Default

Ick bein.

Ok..ok...it's bubble sort...cept...it's not sorting.

A faster way could be the following...

Create a new list.

Loop through the source list (just once).

Check to see if the new list contains the item your adding, if it does, don't add it.

And listen...If your throwing away fruit, I'll take the extras.
Reply With Quote
  #7  
Old 07-26-2006, 12:37 PM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default

Quote:
Originally Posted by Diesel
Ick bein.

Ok..ok...it's bubble sort...cept...it's not sorting.

A faster way could be the following...

Create a new list.

Loop through the source list (just once).

Check to see if the new list contains the item your adding, if it does, don't add it.

And listen...If your throwing away fruit, I'll take the extras.

Nope not throwing away fruit... fact is the list is Cities not fruit

Im also using a ComboBox now instead of a listbox .... The Cities are added this way...

On Form Load I loop through the records of an xmlFile and each town or City found is placed inside the combobox..... Since some of the members of the addressbook that is contained in this xmlFile live in the same towns and or Cities the comboBox gets duplicates thus the need to remove the duplicates

The code I got here does that efficiently....


Oh, and sorry again about not giving you any fruit

vbMarKO
Reply With Quote
  #8  
Old 07-26-2006, 01:17 PM
Cags's Avatar
Cags Cags is offline
Contributor

Preferred language:
C#, VB.NET if nec
 
Join Date: Feb 2004
Location: Melton Mowbray, England
Posts: 695
Cags is on a distinguished road
Default

I personally would have thought it easier and perhaps more efficient to test for a city before adding it to the list in the first place. What I mean by this is that when you are parsing through the xml loading in the names you check ComboBox.Items.Contains() before adding it.
__________________
Anybody looking for a graduate programmer (Midlands, England)?
Reply With Quote
  #9  
Old 07-26-2006, 02:14 PM
Denaes's Avatar
Denaes Denaes is offline
Senior Contributor
 
Join Date: Jun 2003
Posts: 956
Denaes is on a distinguished road
Default

I do exactly this at various dynamic combo filters I have for grids. I just check to see if it's in the list, if so I don't add it.
Reply With Quote
  #10  
Old 07-26-2006, 02:27 PM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default

Quote:
Originally Posted by Cags
I personally would have thought it easier and perhaps more efficient to test for a city before adding it to the list in the first place. What I mean by this is that when you are parsing through the xml loading in the names you check ComboBox.Items.Contains() before adding it.
I agree, except in this case at Form Load there is nothing in the ComboBox... its empty...

The data is gathered from the the xmlFile via a loop... I am looping through each Individuals Record stored in the xmlFile and finding what City they reside in the adding those Cities to the Combo list ....

Since they are being added and then I needed a way to remove duplicates since many of the people on record live in the same Cities...

Ofcourse this wouldnt be needed if when looping through the xmlFIle and gathering cities I had it separate duplicates then only add Cities or towns without any duplicates but sense I am not sure how to do that I thought it easier to simply add them all then add a simple duplicate removal routine of which was offred here and quite simple..

SO the end result is, at Form Load it only adds single instances of each town or City.

vbMarkO
Reply With Quote
  #11  
Old 07-26-2006, 02:44 PM
Cags's Avatar
Cags Cags is offline
Contributor

Preferred language:
C#, VB.NET if nec
 
Join Date: Feb 2004
Location: Melton Mowbray, England
Posts: 695
Cags is on a distinguished road
Default

I don't think your quite understanding how easy it is todo. The fact that your comboBox is empty to start with is fine.

It's really not complicated, in my opinion its simpler than the method your using. Imagine an example using coloured sweets such as smarties. You want a set of sweets containing one of each colour...

a.) you put all the sweets on your pile one at a time, then remove any duplicates one at a time. Thus you handle each sweets many times.

b.) you take each sweet out of it's packet, see if you have one of that colour in the pile, if you do then just ignore the sweet (drop it or eat it, whatever) if you don't you add that sweet to the pile.

You are currently doing b. to do b. all you need to do is anywhere in the loop of your FormLoad where you call comboBox1.Items.Add(myCity), you replace it with
Code:
If comboBox1.Items.Contains(myCity) = False Then
    comboBox1.Items.Add(myCity)
End If

NB. I normally use C# the syntax should be at least close enough for you to understand though.
__________________
Anybody looking for a graduate programmer (Midlands, England)?
Reply With Quote
  #12  
Old 07-26-2006, 04:45 PM
vbMarkO vbMarkO is offline
Centurion
 
Join Date: Aug 2005
Posts: 157
vbMarkO is on a distinguished road
Default

Your right I understand what you mean now sorry for the misuderstanding...

This does look like amuch simpler way for doing it I will give it a try....

thanx

vbMarkO
Reply With Quote
  #13  
Old 10-13-2008, 07:58 PM
Fhwoarang Fhwoarang is offline
Banned

Preferred language:
C#, VB.NET
 
Join Date: Aug 2008
Posts: 24
Fhwoarang is on a distinguished road
Default Re: Remove Duplicate items from a ComboBox How?

Quote:
Originally Posted by mjb3030 View Post
Code:
ComboBox1.Items(i).ToString
How do you this on C#? A way to loop through the values of a ComboBox using a for

Seems like if I add an ( ) after Items, C# doesn't know what am I doing.
Reply With Quote
  #14  
Old 10-13-2008, 08:49 PM
Diesel's Avatar
Diesel Diesel is offline
Contributor
 
Join Date: Aug 2003
Posts: 662
Diesel is on a distinguished road
Default Re: Remove Duplicate items from a ComboBox How?

Are you not using the Contains function? No need to loop through.

Anyway, the indexer syntax in C# uses [].

I never remembered learning german. Not bad.
Reply With Quote
  #15  
Old 10-13-2008, 09:14 PM
Fhwoarang Fhwoarang is offline
Banned

Preferred language:
C#, VB.NET
 
Join Date: Aug 2008
Posts: 24
Fhwoarang is on a distinguished road
Default Re: Remove Duplicate items from a ComboBox How?

Quote:
Originally Posted by Diesel View Post
Are you not using the Contains function? No need to loop through.

Anyway, the indexer syntax in C# uses [].
Oh, man, you are right. I totally forgot about that. Being a former VB6 user screws you up, like using "=" instead "==".

Yeah, I'm using the Cointains method, it is awesome. It is indeed a very nice trick. But I still wanted to know how to access each index individually.

Thank you very much for your soon response.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicate items in a ComboBox bjwade62 Windows Forms 2 02-17-2006 01:20 PM
Remove Checked Items in a Checkedlistbox Lanc1988 Windows Forms 3 02-06-2005 10:08 PM
Remove all currently selected listbox items micropathic Windows Forms 3 06-06-2004 10:17 PM
form2.listbox.Items = form1.Combobox.Items torg Windows Forms 3 04-19-2003 07:06 AM
Remove selection from ComboBox item jhasty Windows Forms 4 03-10-2003 08:30 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe