Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove duplicate from ListBox

76 views
Skip to first unread message

Liam Whan

unread,
May 27, 2012, 8:34:55 AM5/27/12
to
Hi All,

I'm sure this is easy to answer but I can not figureit out.

I am getting an OutOfBoundException when I run this loop that loops
through the listbox in question and looks for duplicate values. Could
anyone shed any light on this?


For intListCountF As integer = 0 to frmPlayer.Listbox1.ListCount -1
For intListCountB As Integer = frmPlayer.Listbox1.Listcount -1 to 0
Step -1
if frmplayer.Listbox1.List(intListCountB) =
frmPlayer.Listbox1.List(intListCountF) Then
frmplayer.Listbox1.RemoveRow(intListCountB)
end if
Next
Next

Cheers,
Liam.

Dale Arends

unread,
May 27, 2012, 5:20:26 PM5/27/12
to
Liam Whan <liam...@gmail.com> wrote in news:4fc21f6f$0$1762$c3e8da3
$92d0...@news.astraweb.com:
Because you're reducing the number of rows in the listbox while still
indexing on the original number of rows. At some point you will try to
access a row number that no longer exists in the listbox.


--
Dale M. Arends
dalea...@sbcglobal.net

Auric__

unread,
May 27, 2012, 5:41:46 PM5/27/12
to
You're clearing your list. When intListCountF has the same value as
intListCountB, you're removing the item. Since you will get to *every* value
in the list in both For loops, you will remove *everything*. Eventually
you're left with a list containing zero items.

Try something like this (untested air code):

For intListCountF As Integer = 0 To frmPlayer.Listbox1.ListCount - 2
For intListCountB As Integer = frmPlayer.Listbox1.ListCount - 1 To _
intListCountF + 1 Step -1
If frmplayer.Listbox1.List(intListCountB) = _
frmPlayer.Listbox1.List(intListCountF) Then
frmplayer.Listbox1.RemoveRow(intListCountB)
End If
Next
Next

--
- Can we talk about this next week?
- Just one more thing...
- Is it next week already?!

Liam Whan

unread,
Jun 1, 2012, 9:31:32 PM6/1/12
to
Of Course!

I'm an idiot! Thanks so much guys.

what I eventually went with was an algorithim that -1 off the row count
every time it removed a row.

Thanks again!

Liam

0 new messages