Board index » delphi » TListBox - How do I Deselect all Selected Items in a TListBox

TListBox - How do I Deselect all Selected Items in a TListBox

Help

How do I Deselect all of the Selected Items in a TListBox?

Here is the code I am attempting to use but I get a 'List index out of
bounds (0)' error message.

{----------------------------------------------------------------------------------------------}

for I := 0 to (Listbox.Items.Count - 1) do
    begin
            ListBox.Selected[i] := False;
    end;

{----------------------------------------------------------------------------------------------}

Any help will be greatly appreciated.

Mark Moss
markm...@ix.netcom.com

 

Re:TListBox - How do I Deselect all Selected Items in a TListBox


Your code works if you have MultiSelect  =  True.

If not,

for I := 0 to (Listbox1.Items.Count - 1) do
     if ListBox1.Selected[i] then
       ListBox1.Selected[i]  =  False;

Works fine. You could also jump out of the loop in this case when the
selection has been found as no other item will be if MultiSelect  =  False;

Don't ask me why!

Re:TListBox - How do I Deselect all Selected Items in a TListBox


Martin

When I use you code ( which is very much like mine ) I still get the same error
'List Index out of bounds',

I am using Windows NT and Delphi 4.0 with update pack 3 installed

Mark Moss

P.S. - I created a test program that has a button that performs the deselect, a
list box with five items in it and an oncreate which loads the 5 items into the
listbox.  And still I get the Out of Bounds error.

Quote
Martin Moore wrote:
> Your code works if you have MultiSelect  =  True.

> If not,

> for I := 0 to (Listbox1.Items.Count - 1) do
>      if ListBox1.Selected[i] then
>        ListBox1.Selected[i]  =  False;

> Works fine. You could also jump out of the loop in this case when the
> selection has been found as no other item will be if MultiSelect  =  False;

> Don't ask me why!

Re:TListBox - How do I Deselect all Selected Items in a TListBox


procedure TForm1.Button1Click(Sender: TObject);
var
  I : integer;
begin
  if ListBox.MultiSelect then
    for I := 0 to (Listbox.Items.Count - 1) do
      ListBox.Selected [i] := False
  else
    ListBox.ItemIndex := -1;
end;

On Wed, 22 Sep 1999 09:59:18 -0600, "Mark E. Moss"

Quote
<markm...@ix.netcom.com> wrote:
>Help

>How do I Deselect all of the Selected Items in a TListBox?

>Here is the code I am attempting to use but I get a 'List index out of
>bounds (0)' error message.

>{----------------------------------------------------------------------------------------------}

>for I := 0 to (Listbox.Items.Count - 1) do
>    begin
>            ListBox.Selected[i] := False;
>    end;

>{----------------------------------------------------------------------------------------------}

>Any help will be greatly appreciated.

>Mark Moss
>markm...@ix.netcom.com

Re:TListBox - How do I Deselect all Selected Items in a TListBox


Pekka

Thank you very much for the information, it worked great.

Is this documented anywhere?

Mark Moss

Quote
Pekka Haapalainen wrote:
> procedure TForm1.Button1Click(Sender: TObject);
> var
>   I : integer;
> begin
>   if ListBox.MultiSelect then
>     for I := 0 to (Listbox.Items.Count - 1) do
>       ListBox.Selected [i] := False
>   else
>     ListBox.ItemIndex := -1;
> end;

> On Wed, 22 Sep 1999 09:59:18 -0600, "Mark E. Moss"
> <markm...@ix.netcom.com> wrote:

> >Help

> >How do I Deselect all of the Selected Items in a TListBox?

> >Here is the code I am attempting to use but I get a 'List index out of
> >bounds (0)' error message.

> >{----------------------------------------------------------------------------------------------}

> >for I := 0 to (Listbox.Items.Count - 1) do
> >    begin
> >            ListBox.Selected[i] := False;
> >    end;

> >{----------------------------------------------------------------------------------------------}

> >Any help will be greatly appreciated.

> >Mark Moss
> >markm...@ix.netcom.com

Re:TListBox - How do I Deselect all Selected Items in a TListBox


Nah, at least I did not found anything on docs... It seems to be
Windows problem and Win32 help didn't mention any limits on the
function...

On Thu, 23 Sep 1999 14:06:36 -0600, "Mark E. Moss"

Quote
<markm...@ix.netcom.com> wrote:
>Pekka

>Thank you very much for the information, it worked great.

>Is this documented anywhere?

>Mark Moss

Re:TListBox - How do I Deselect all Selected Items in a TListBox


Why don't you use LB_SELITEMRANGE for Multiselect-Listboxes?

***
An application sends an LB_SELITEMRANGE message to select one or more
consecutive items in a multiple-selection list box.

LB_SELITEMRANGE
wParam = (WPARAM) (BOOL) fSelect;   // selection flag
lParam = MAKELPARAM(wFirst, wLast); // first and last items
***

The VCL uses also LB_Xxx-Messages to select one item. But this is more
efficient than deselecting each item separately.

JensG

Other Threads