Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ListBox Unselect - How?

2 views
Skip to first unread message

Nick Schmitz

unread,
Aug 20, 1996, 3:00:00 AM8/20/96
to

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.

Brien King

unread,
Aug 21, 1996, 3:00:00 AM8/21/96
to

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

Lord of Darkness

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

> for j:= 0 to ListBox1.Items.Count - 1
> if ListBox1.Selected[j] then
> ListBox1.Selected[j] := False;
>

alternatively

ListBox1.ItemIndex := -1;

deselects all of them

John B

James Leo Woodford, Jr.

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

You also might want to

ListBox.Items.ItemIndex := -1

to deselect all items in the listbox.

TomCorcora

unread,
Aug 25, 1996, 3:00:00 AM8/25/96
to

<<You also might want to

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.


Mark Pritchard

unread,
Aug 26, 1996, 3:00:00 AM8/26/96
to TomCorcora

ItemIndex isn't applicable. I tested the -1 set, and it didn't work.

--
Mark.

C8591

unread,
Sep 3, 1996, 3:00:00 AM9/3/96
to

> From: Nick Schmitz <nick.s...@amd.com>

> 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

0 new messages