I suppose that you want to find the position of the first item that begins with a
given search string. You can check each item of the listbox with the Delphi
function Pos(SubString, S) that returns the position of a given "substring" in a given string S. It
returns 1 or higher if SubString is contained in S, or -1 if SubString is not found in S.
The example function below returns the index of the item that begins with a certain search
string. It returns from 0 to the number of items minus one, or it returns -1 if no item
is found that begins with SearchStr. We also provided for a safety net in case that the
listbox is empty, and we made the checking "case insensitive":
function TForm1.ItemNrBeginsWith(SearchStr: string): integer;
var
i, P: integer;
begin
Result := -1;
if ListBox1.Items.Count = 0 then exit;
for i := 0 to ListBox1.Items.Count - 1 do
if Pos(LowerCase(S), LowerCase(ListBox1.Items[i])) = 1 then begin
Result := i;
break;
end;
end;
How to use this function:
var
ItemNr: integer;
begin
...
ItemNr := ItemNrBeginsWith(Edit1.Text);
...