Hello, I have a problem with the following code:
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.