Distill Duplicate Elements in Drop Down in vb.net

G

Guest

Hi I'm trying to remove duplicate elements from
a Drop Down List Fill in VB.net.

Following code worked well with vb6. But I'm getting index out of range if I
try to translate it to vb.net code at line#4. Any possible clues will be
appreciated. Thanks.

'To distill the Combo Element
NumY = Combo5.ListCount + 1 '''''''''''''''Line1
For NumX = 1 To Combo5.ListCount '''''''line2
NumY = NumY - 1 '''''''line3
If (Combo5.List(NumY) = Combo5.List(NumY - 1)) Then
''''''line4
Combo5.RemoveItem (NumY) '''''''''''''line5
End If '''''line6
Next ''''line7
---------------In vb.net---------------
 
K

Ken Tucker [MVP]

Hi,

This code will add 20 items to a combobox and remove the last 18.
When removing an item by number start at the end of the list and work
forward to prevent referencing an item that isnt there anymore.

For x As Integer = 1 To 20

ComboBox1.Items.Add(x.ToString)

Next

For i As Integer = ComboBox1.Items.Count - 1 To 2 Step -1

ComboBox1.Items.RemoveAt(i)

Next



Ken

-------------------------

Hi I'm trying to remove duplicate elements from
a Drop Down List Fill in VB.net.

Following code worked well with vb6. But I'm getting index out of range if I
try to translate it to vb.net code at line#4. Any possible clues will be
appreciated. Thanks.

'To distill the Combo Element
NumY = Combo5.ListCount + 1 '''''''''''''''Line1
For NumX = 1 To Combo5.ListCount '''''''line2
NumY = NumY - 1 '''''''line3
If (Combo5.List(NumY) = Combo5.List(NumY - 1)) Then
''''''line4
Combo5.RemoveItem (NumY) '''''''''''''line5
End If '''''line6
Next ''''line7
---------------In vb.net---------------
 
G

Guest

Hi,
I'm actually trying to compare the elements within a drop down list.
If say #18 & #19 are same I want to remove #19.

Question is how do I make the elemental comparision?
And then delete the ;ast repeating element(s).

In vb6 I would do:
If (Combo5.List(NumY) = Combo5.List(NumY - 1)) Then

In vb.net how do I go about.?

Thanks,
 
K

Ken Tucker [MVP]

Hi,

For x As Integer = 1 To 20

ComboBox1.Items.Add(x.ToString)

Next

ComboBox1.Items.Add("1")

ComboBox1.Sorted = True

For i As Integer = ComboBox1.Items.Count - 1 To 1 Step -1

If ComboBox1.Items.Item(i).Equals(ComboBox1.Items.Item(i - 1)) Then

ComboBox1.Items.RemoveAt(i)

End If

Next



Ken

-------------------------

Hi,
I'm actually trying to compare the elements within a drop down list.
If say #18 & #19 are same I want to remove #19.

Question is how do I make the elemental comparision?
And then delete the ;ast repeating element(s).

In vb6 I would do:
If (Combo5.List(NumY) = Combo5.List(NumY - 1)) Then

In vb.net how do I go about.?

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top