[Gambas-user] ListBox selection

tobias tobiasboe1 at ...20...
Wed Jun 15 20:25:50 CEST 2011


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?




More information about the User mailing list