If listbox.count - out of index error

mdgarf

Member
Licensed User
Longtime User
Hi, I am getting error and do not really understand why.

I have listbox and buttons to move items up and down. In if condition I get error index out of range when first item is selected and when second last is selected I am unable to create condition for append/add instead of insert, same problem with if condition.

I must by doing something really wrong.

Thanks for help.
mirek

If Not((listbox1.SelectedIndex = (listbox1.Count-1)) OR (listbox1.SelectedIndex = -1)) Then
temp=listbox1.SelectedIndex
tempstr=listbox1.Item(temp)
listbox1.Removeat(temp)
listbox1.Insert(temp+1,tempstr)
Listbox1.SelectedIndex=temp+1
Else If
Sound("")
End If
 

mjcoon

Well-Known Member
Licensed User
I'm not certain that I understand what the code is meant to do, but are you taking account that as soon as you remove an item that index position now contains what was the original following item? Also at that moment listbox1.Count decreases by one.

I don't understand, either, why you are testing for "listbox1.SelectedIndex = -1"; can that ever be true?

Another possibility is to remove the up and/or down buttons from the screen when there is only one item in the list and when the selected item doesn't allow that direction of movement. Then (theoretically!) you would not have to do the test in the code. But you may not be using event SelectionChanged (because that will occur even when you change the SelectedIndex property in the code).

Mike.
 

agraham

Expert
Licensed User
Longtime User
This superfluous "If" seems to be the problem but the error message is incredibly misleading . Erel!
B4X:
  Listbox1.SelectedIndex=temp+1
Else [COLOR="Red"]If[/COLOR]
  Sound("")
@Mike, the code does work as he intended to move items up and down. The index can be -1 if no item is selected in the Listbox so the presence of it in the conditional is quite valid.
 
Last edited:

mjcoon

Well-Known Member
Licensed User
@Mike, the code does work as he intended to move items up and down. The index can be -1 if no item is selected in the Listbox so the presence of it in the conditional is quite valid.

Thanks Andrew, I failed to spot the extra "If"!

Also, after writing my twopen'rth, I thought the code would be OK for a MoveDown, but that the initial test should also exclude listbox1.SelectedIndex = (listbox1.Count-2) otherwise there won't be a following item to exchange with. Am I getting nearer...

I did some similar code for moving items in a Table, somewhat handicapped by there not being an Insert method. I had thought that there, too, there could be no case of an unselected item and a value of "-1", since it is not in the help. Something else to reconsider...

Mike.
 

mdgarf

Member
Licensed User
Longtime User
Thanks for fast response. I am new to this and was hunting wrong bug. Error was "index out of range" on if statement so I assumed problem was in condition evaluation.
It now works nicely.
thanks
mirek
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…