Menu

#75 TVpContacts.FindContactByName

open
nobody
Contacts (13)
5
2013-01-15
2003-05-21
No

function TVpContacts.FindContactByName does not
consider the value of SortBy property for search
contacts in ContactGrid, It always make a search in
LastName.

Below, the correct code:

function TVpContacts.FindContactByName(const Name:
string;
CaseInsensitive: Boolean): TVpContact;
var
I: Integer;
SearchStr: String;
SearchLength: Integer;
SrcStr: String;
begin
Result := nil;

{ to enhance performance, uppercase the input name }
{ and get its length only once }
if CaseInsensitive then
SearchStr := uppercase(Name)
else
SearchStr := Name;
SearchLength := Length(SearchStr);

{ Iterate the contacts looking for a match }
for I := 0 to FContactsList.Count - 1 do begin
// --------->>
if FContactSort = csFirstLast then
SrcStr := TVpContact(FContactsList.List^
[I]).FirstName
else
SrcStr := TVpContact(FContactsList.List^
[I]).LastName;
//-------------->>
if CaseInsensitive then begin
{ not case sensitive }
//-------------->>
if UpperCase(Copy(SrcStr, 1, SearchLength)) =
SearchStr then begin
//-------------->>
{ we found a match, so return it and bail out }
Result := FContactsList.Items[I];
Exit;
end;
end else begin
{ case sensitive }
//-------------->>
if Copy(SrcStr, 1, SearchLength) = SearchStr then
begin
//-------------->>
{ we found a match, so return it and bail out }
Result := FContactsList.Items[I];
Exit;
end;
end;
end;
end;

Discussion


Log in to post a comment.