The following code gives me a List Index out-of-bound error:
for j:=1 to ListBox1.Items.Count-1 do
if ListBox1.Selected[j] then
ListBox1.Selected[j]:=false;
I tried looking in Help & the printed doc, but nothing was obvious.
Any hints for a Delphi Newbie? TIA.
Two things, first you should use:
for j:=1 to ListBox1.Items.Count
if ListBox1.Selected[j-1] then
ListBox1.Selected[j-1] := False;
or
for j:= 0 to ListBox1.Items.Count - 1
if ListBox1.Selected[j] then
ListBox1.Selected[j] := False;
otherwise you will miss the last element in the list box. Second one
is more efficient since it doesn't have to do two calculations for
(j-1).
The other thing you need to do is make sure the ListBox has
MultiSelect set to True. If it is not, you will get a List Index out
of bounds error when you do the "ListBox1.Selected[j] := False;"
Oddly enough, the "If ListBox1.Selected[j] then" does NOT generate
this error.
Brien King
bk...@primenet.com
alternatively
ListBox1.ItemIndex := -1;
deselects all of them
John B
ListBox.Items.ItemIndex := -1
to deselect all items in the listbox.
ListBox.Items.ItemIndex := -1
to deselect all items in the listbox.
>>
I thought itemIndex wasn't applicable when multiselcet is set? Can someone
explain the difference please.
Thanks.
Tom.
ItemIndex isn't applicable. I tested the -1 set, and it didn't work.
--
Mark.
> I've got a simple application using a list box; sometimes I want
> to do something on the selected item, other times I want the
> Delphi program to unselect items & do something on the whole list.
> The following code gives me a List Index out-of-bound error:
> for j:=1 to ListBox1.Items.Count-1 do
> if ListBox1.Selected[j] then
> ListBox1.Selected[j]:=false;
> I tried looking in Help & the printed doc, but nothing was obvious.
> Any hints for a Delphi Newbie? TIA.
Try
ListBox1.MultiSelct := false;
ListBox1.ItemIndex := -1;
ListBox1.MultiSelct := true;
Thanks,
Chris
c8...@aol.com