Recent

Author Topic: [SOLVED] ListBox selected item does not appear in the visible part of the list  (Read 4103 times)

pasquale

  • Sr. Member
  • ****
  • Posts: 271
    • Esposito Software
Hello, I have a problem with the following code:

Code: [Select]

OldSearch: string; 

procedure TfrmResearch.cmdSearchClick(Sender: TObject);
var
  SearchFor: string;
  StartAt, i: integer;
  Found: Boolean;
begin

try

  If lstRecords.Items.Count = 0 Then
  begin
    ShowMessage('No items on the list!');
  Exit;
  end;

  If lstRecords.ItemIndex = -1 Then lstRecords.ItemIndex := 0;

  SearchFor := LowerCase(txtSearchString.Text);
  if (SearchFor <> OldSearch) then StartAt := 0
  else StartAt := lstRecords.ItemIndex;
    i := StartAt;
  if (SearchFor <> OldSearch) then
    Found := Pos(SearchFor, LowerCase(lstRecords.Items[i])) > 0
  else
    Found := False;
  while not Found do begin
    inc(i); // next item
    // wrap around if needed
    if i > lstRecords.Items.Count - 1 then i := 0;
      Found := Pos(SearchFor, LowerCase(lstRecords.Items[i])) > 0;
    if i = StartAt then break;
  end;

  if Found then lstRecords.ItemIndex := i;
  OldSearch := SearchFor;

except
  exit;
end;

end;       

The code above selects the item in the ListBox containing the string indicated in txtSearchString but the problem is that the item selected does not appear in the visible part of the list.

Do you know how to solve this problem?

Thanks in advance.
« Last Edit: March 31, 2012, 09:33:58 am by pasquale »
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

pasquale

  • Sr. Member
  • ****
  • Posts: 271
    • Esposito Software
Re: ListBox selected item does not appear in the visible part of the list
« Reply #1 on: March 31, 2012, 09:33:33 am »
Problem solved!

It was enough to add the following lines of code at the bottom of the procedure:

Code: [Select]
  lstRecords.TopIndex := lstRecords.ItemIndex;
  lstRecords.SetFocus; 
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

pasquale

  • Sr. Member
  • ****
  • Posts: 271
    • Esposito Software
By the way, I forgot to tell you that the problem above only occurs under Mac. When programming for Windows, the item selected automatically appears in the visible part of the list and you don't need to use the last couple of lines of code.
Apple loves breaking backward compatibility to make money. If you want to be sure that your apps will never stop working, use Windows and trash macOS!

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
While this is an old post, I thought to mention that the same problem also (still) exists on Linux, in my case Manjaro. I have to explicitly set the TopIndex in order for the selected item to appear in the visible part of the list.
It's only logical.

 

TinyPortal © 2005-2018