By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,262 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,262 IT Pros & Developers. It's quick & easy.

Distill Duplicate Elements in Drop Down in vb.net

P: n/a
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---------------

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

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

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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---------------
Nov 21 '05 #2

P: n/a
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,


"Ken Tucker [MVP]" wrote:
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

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

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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---------------

Nov 21 '05 #3

P: n/a
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.Ite m(i - 1)) Then

ComboBox1.Items.RemoveAt(i)

End If

Next

Ken

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

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...
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,


"Ken Tucker [MVP]" wrote:
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

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

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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---------------

Nov 21 '05 #4

P: n/a
Gurvar,

When you are using a datasoure

http://groups.google.com/groups?selm...TNGP15.phx.gbl

I had this link still in my clipboard, because I showed it to Rodchar before
you

Cor
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.