Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Visual Basic 2008 Express Edition!
Posted
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

Posted

This worked perfect....

 

But if you dont mind... might I ask you a question or 2 about this 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

Visual Basic 2008 Express Edition!
Posted
This worked perfect....

 

But if you dont mind... might I ask you a question or 2 about this 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.

 

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.

Posted

Thank You that makes sense now...

 

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

 

vbMarkO

Visual Basic 2008 Express Edition!
Posted

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.

Posted
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

Visual Basic 2008 Express Edition!
Posted
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)?
Posted
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

Visual Basic 2008 Express Edition!
Posted

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

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)?
Posted

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

Visual Basic 2008 Express Edition!
  • 2 years later...
Guest Fhwoarang
Posted
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.

Posted

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.

Guest Fhwoarang
Posted
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. :D 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...