Board index » delphi » FileListBox question

FileListBox question

Hi to all and thanks for any replay's

I have in a filelistbox about 200 (Stock) files.
If the listbox has focus and I press a key, for example a "g" then I
get the first file starting with "g" at the bottom of the filelistbox
(highlighted).

Here comes the question: What have I to do,  to get this files at the
top of the filelistbox?

I did try to pass the Flbox.itemindex to Flbox.topindex.
The result was that if I pressed a "g" the first file starting with
"g" appeared at the bottom. if I pressed "g" again, it moved to the
top, and if I pressed then a "m", then{*word*128}file staring with "m"  was
some where of the screen.

Regards Gery

www.zipworld.com.au~gprdata

 

Re:FileListBox question


Hi Gery,

Just had a play around and found that if you don't attach any code to the
listbox it will automatically jump to the one starting with that letter,
although not at the top of the list!  This suggests to me that the Listbox
component has some code already in it to manage this.

Try this - it seems to work fine for me - in particular note the Key:=#0
line!

procedure TForm1.ListBox1KeyPress(Sender: TObject; var Key: Char);
var
   c : word;
begin
     // Assume list box is sorted
     // Find the first item starting with 'Key' - note case sensitive...
     for c:=0 to ListBox1.Items.Count-1 do
          if ListBox1.Items[c][1]=Key then Break;

     ListBox1.ItemIndex:=C;
     ListBox1.TopIndex:=C;

     Key:=#0;  // Toss away the keystroke - don't let the listbox see it!
end;

Cheers,

- Bob -

Quote
Gery Rohrig wrote in message <399faab5.2062...@news.zipworld.com.au>...
>Hi to all and thanks for any replay's

>I have in a filelistbox about 200 (Stock) files.
>If the listbox has focus and I press a key, for example a "g" then I
>get the first file starting with "g" at the bottom of the filelistbox
>(highlighted).

>Here comes the question: What have I to do,  to get this files at the
>top of the filelistbox?

>I did try to pass the Flbox.itemindex to Flbox.topindex.
>The result was that if I pressed a "g" the first file starting with
>"g" appeared at the bottom. if I pressed "g" again, it moved to the
>top, and if I pressed then a "m", then{*word*128}file staring with "m"  was
>some where of the screen.

>Regards Gery

>www.zipworld.com.au~gprdata

Re:FileListBox question


You are forgetting error-checking:

procedure TForm1.ListBox1KeyPress(Sender: TObject; var Key: Char);
var
  i: Integer;
begin
  with Sender as TListBox do
    for i := 0 to Items.Count - 1 do
      if (Length(Items[i]) > 0) and (Items[i][1] = Key) then
      begin
        ItemIndex := i;
        TopIndex := i;
        Key := #0;
        Break;
      end;
end;

"Bob Brown" <nab...@opus.co.nz> schreef in bericht
news:8omqr1$3f6$1@com.opus.co.nz...

Quote
> Hi Gery,

> Just had a play around and found that if you don't attach any code to the
> listbox it will automatically jump to the one starting with that letter,
> although not at the top of the list!  This suggests to me that the Listbox
> component has some code already in it to manage this.

> Try this - it seems to work fine for me - in particular note the Key:=#0
> line!

> procedure TForm1.ListBox1KeyPress(Sender: TObject; var Key: Char);
> var
>    c : word;
> begin
>      // Assume list box is sorted
>      // Find the first item starting with 'Key' - note case sensitive...
>      for c:=0 to ListBox1.Items.Count-1 do
>           if ListBox1.Items[c][1]=Key then Break;

>      ListBox1.ItemIndex:=C;
>      ListBox1.TopIndex:=C;

>      Key:=#0;  // Toss away the keystroke - don't let the listbox see it!
> end;

> Cheers,

> - Bob -

> Gery Rohrig wrote in message <399faab5.2062...@news.zipworld.com.au>...
> >Hi to all and thanks for any replay's

> >I have in a filelistbox about 200 (Stock) files.
> >If the listbox has focus and I press a key, for example a "g" then I
> >get the first file starting with "g" at the bottom of the filelistbox
> >(highlighted).

> >Here comes the question: What have I to do,  to get this files at the
> >top of the filelistbox?

> >I did try to pass the Flbox.itemindex to Flbox.topindex.
> >The result was that if I pressed a "g" the first file starting with
> >"g" appeared at the bottom. if I pressed "g" again, it moved to the
> >top, and if I pressed then a "m", then{*word*128}file staring with "m"  was
> >some where of the screen.

> >Regards Gery

> >www.zipworld.com.au~gprdata

Other Threads