Removing duplicates in combobox - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Removing duplicates in combobox

Posts: 6Member
I thought the following code would work, but somehow it doesn't. I get an error on "combobox.removeitem a". I can't figure out what's wrong.

dim i, a, c as integer

For i = 0 To (c - 1)
For a = 1 To c
If combobox.List(i) = combobox.List(a) Then
combobox.RemoveItem a
a = a - 1
c = c - 1
End If
Next a
Next i

• Posts: 937Member
: I thought the following code would work, but somehow it doesn't. I get an error on "combobox.removeitem a". I can't figure out what's wrong.
:
: dim i, a, c as integer
:
: For i = 0 To (c - 1)
: For a = 1 To c
: If combobox.List(i) = combobox.List(a) Then
: combobox.RemoveItem a
: a = a - 1
: c = c - 1
: End If
: Next a
: Next i
:

Hi!

It looks like you forgot to put a in (). like this:
combobox.RemoveItem (a)

By the way, I re-coded it, and I think it is a little clearer this way, and a bit more efficient:

[code]
For i = 0 To List1.ListCount - 1
For n = i + 1 To List1.ListCount - 1
If List1.List(i) = List1.List(n) Then
List1.RemoveItem (n)
n = n - 1
End If
Next n
Next i
[/code]

You also have a "gotcha" in this line:
[code]
dim i, a, c as integer
[/code]
In fact, this creates two variants, i and a, and one integer, c

You probably meant to do this:
[code]
Dim I as Integer, A as Integer, C as Integer
[/code]

Personally, I prefer this way:

[code]
Dim I as Integer
Dim A as Integer
Dim C as Integer

Happy Coding!

Melissa

• Posts: 828Member
: : I thought the following code would work, but somehow it doesn't. I get an error on "combobox.removeitem a". I can't figure out what's wrong.
: :
: : dim i, a, c as integer
: :
: : For i = 0 To (c - 1)
: : For a = 1 To c
: : If combobox.List(i) = combobox.List(a) Then
: : combobox.RemoveItem a
: : a = a - 1
: : c = c - 1
: : End If
: : Next a
: : Next i
: :
:
: Hi!
:
: It looks like you forgot to put a in (). like this:
: combobox.RemoveItem (a)
:
: By the way, I re-coded it, and I think it is a little clearer this way, and a bit more efficient:
:
: [code]
: For i = 0 To List1.ListCount - 1
: For n = i + 1 To List1.ListCount - 1
: If List1.List(i) = List1.List(n) Then
: List1.RemoveItem (n)
: n = n - 1
: End If
: Next n
: Next i
: [/code]
:
: You also have a "gotcha" in this line:
: [code]
: dim i, a, c as integer
: [/code]
: In fact, this creates two variants, i and a, and one integer, c
:
: You probably meant to do this:
: [code]
: Dim I as Integer, A as Integer, C as Integer
: [/code]
:
: Personally, I prefer this way:
:
: [code]
: Dim I as Integer
: Dim A as Integer
: Dim C as Integer
:
:
:
:
: Happy Coding!
:
: Melissa
:
: [/code]

Firstly you do not need the brackets when a sub or function is called and is not on the right hand side of a = sign. Infact if you call a sub which has two or more arguments in it, put brackets around the arguments, and the sub is not on the right hand side of a = sign, visual basic will run a error. So the below code is perfectly OK:

[code]combobox.RemoveItem a[/code]

Anyway when deleting multiple items in a combo box, you need to delete the items at the end of the list first. What you are actually doing is deleting items at the start of the list, and then trying to delete items at the end of the list which do not exist. The following code will work:

[code]Dim a As Integer, b As Integer, c As Integer
a = Combo1.ListCount - 1
Do While a >= 0
For b = a - 1 To 0 Step -1
If (Combo1.List(b) = Combo1.List(a)) Then
Combo1.RemoveItem b
a = a - 1
End If
Next b
a = a - 1
Loop[/code]

• Posts: 937Member

I tested the code I posted, with anywhere from 10 to 1000 items, and it worked every time. Did it fail for you? Was my testing flawed as well as my code?

Melissa

• Posts: 828Member
:
: I tested the code I posted, with anywhere from 10 to 1000 items, and it worked every time. Did it fail for you? Was my testing flawed as well as my code?
:
: Melissa
:
:
:
:
:

Most of what I was saying in my post was actually directed at Henrik. I just replied to your answer because I was correcting what you said about the brackets. However, the problems I mentioned with the code were actually Henrik's code not yours.

I didn't actually look at your code, because I thought that you were saying that the lack of brackets was causing the error. Therefore I assumed that the only difference between the original code, and your code was that your code had brackets. Therefore I posted my code.

I apologise for not making myself clear. I probably shouldn't use words like "You" when I'm referring to someone else other than the person who's message I am replying to.

One day I'll learn not to assume, but in the mean time at least he's got two choices.