Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.basic.misc > Newsgroup comp.lang.basic.realbasic

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-27-2012, 12:34 PM
Liam Whan
Guest
 
Posts: n/a
Default Remove duplicate from ListBox

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.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 05-27-2012, 09:20 PM
Dale Arends
Guest
 
Posts: n/a
Default Re: Remove duplicate from ListBox

Liam Whan <liam.whan@gmail.com> wrote in news:4fc21f6f$0$1762$c3e8da3
$92d0a893@news.astraweb.com:

> 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.
>


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
dalearends@sbcglobal.net
Reply With Quote
  #3 (permalink)  
Old 05-27-2012, 09:41 PM
Auric__
Guest
 
Posts: n/a
Default Re: Remove duplicate from ListBox

Liam Whan wrote:

> 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


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?!
Reply With Quote
  #4 (permalink)  
Old 06-02-2012, 01:31 AM
Liam Whan
Guest
 
Posts: n/a
Default Re: Remove duplicate from ListBox

On 28/05/2012 7:41 AM, Auric__ wrote:
> Liam Whan wrote:
>
>> 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

> 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
>

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

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 07:53 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.