Board index » delphi » Listbox - List Index out of bounds when setting Selected (unless Multiselect)

Listbox - List Index out of bounds when setting Selected (unless Multiselect)

Using D2.0 C/S with Win95
I'm trying to load a TListBox (called lbCodes) and set the selected
property depending upon whether the corresponding record exists in a
table (taANValue). The code I'm using is like ...

      linect:=lbCodes.Items.Add( MyString );
      if dmData.taANValue.FindKey([ ANPrefix, ANRecordFK, ANType,
ANCode]) then
         lbCodes.Selected[linect]:= true
      else
         lbCodes.Selected[linect]:= false;

This works fine if MultiSelect is true, but gives a List Index Out of
Bounds error if false.
Any ideas why, or how to prevent this?
E-mail replies appreciated. TIA :-)

 

Re:Listbox - List Index out of bounds when setting Selected (unless Multiselect)


Quote
David Carle wrote:

> Using D2.0 C/S with Win95
> I'm trying to load a TListBox (called lbCodes) and set the selected
> property depending upon whether the corresponding record exists in a
> table (taANValue). The code I'm using is like ...

>       linect:=lbCodes.Items.Add( MyString );
>       if dmData.taANValue.FindKey([ ANPrefix, ANRecordFK, ANType,
> ANCode]) then
>          lbCodes.Selected[linect]:= true
>       else
>          lbCodes.Selected[linect]:= false;

> This works fine if MultiSelect is true, but gives a List Index Out of
> Bounds error if false.
> Any ideas why, or how to prevent this?
> E-mail replies appreciated. TIA :-)

You guessed it,

You cannot have multiple Selected items unless the MultiSelect
property is true thus when you try to set the next item as
selected you are violating the singleselect constraint.

I would say in this scenario that Multiselect is required.

GregF

Other Threads