[Gambas-user] ListBox selection
Demosthenes Koptsis
demosthenesk at ...626...
Wed Jun 15 21:05:14 CEST 2011
Yes all of you have understood correctly what i want to do.
Of course Bad Index problem appears and for that i implement this code:
---------------------------------
Public Sub btnRemove_Click()
Dim i As Integer
Dim sSelected As New String[]
sSelected.Clear
For i = 0 To lstBox.Count - 1
If lstBox[i].Selected Then
sSelected.Add(lstBox[i].Text)
Endif
Next
For i = 0 To sSelected.Count - 1
lstBox.Remove(lstBox.Find(sSelected[i]))
Next
End
---------------------------------
This code is based on selected text
lstBox.Remove(lstBox.Find(sSelected[i]))
thats why i wanted a lstBox[i].Index.
But i will try the DEC i code to see the deference.
On Wed, 2011-06-15 at 20:25 +0200, tobias wrote:
> hi,
>
> oh well, now i think i understood. but this doesn't change my answer:
> you don't need find to get the index of a special item.
> you said, you selected one or multiple items and you want to remove
> them. no problem, use the method i mentioned earlier in this topic:
> PUBLIC SUB Button1_Click()
>
> DIM i AS Integer
>
> 'Go through all items and find those that are selected
> FOR i = 0 TO ListBox1.Count - 1
> IF ListBox1[i].Selected THEN
> ListBox1.Remove(i)
> ENDIF
> NEXT
>
> END
>
> this code won't work properly, i saw while testing. (it raises "Bad
> Index" - it's "out of bounds") thanks to Benoit's answer i understand
> why. i iterate through all the indices until the initial ListBox1.Count
> which has changed during processing. but shouldn't such a FOR-loop be
> more dynamically? is this FOR-loop end point only evaluated at the
> beginning? or do i miss something else here?
> why does the loop get into the iteration where the error is raised even
> if the condition shouldn't be true? (btw i'm using gambas2.22)
>
> anyway you may use this one instead:
>
> DIM i AS Integer
>
> 'Go through all items and find those that are selected in a while
> which evaluates ListBox1.Count every iteration
> i = 0
> WHILE i < ListBox1.Count
> 'is it selected?
> IF ListBox1[i].Selected THEN
> 'remove and...
> ListBox1.Remove(i)
> 'don't forget to decrement i because the relative offset of the
> next element has changed to the current offset, so one has to process
> the current index again
> DEC i
> ENDIF
> INC i
> WEND
>
> or did i still not get what you wanted?
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes Koptsis.
More information about the User
mailing list