diff --git a/components/tvplanit/source/vpdata.pas b/components/tvplanit/source/vpdata.pas index 2ad690a4c..ebd7ec160 100644 --- a/components/tvplanit/source/vpdata.pas +++ b/components/tvplanit/source/vpdata.pas @@ -493,8 +493,8 @@ type function Count: Integer; procedure DeleteContact(Contact: TVpContact); function First: TVpContact; - function FindContactByName(const AName: string; - CaseInsensitive: Boolean = True): TVpContact; + function FindContactByName(const AName: string; CaseInsensitive: Boolean = True): TVpContact; overload; + function FindContactByName(const AFirstName, ALastName: string; CaseInsensitive: Boolean = True): TVpContact; overload; function FindContactIndexByName(const Name: string; CaseInsensitive: Boolean = True): Integer; function GetContact(Index: Integer): TVpContact; @@ -2987,9 +2987,10 @@ begin end; end; -{ new function introduced to support the new buttonbar component. } +{ Function introduced to support the new buttonbar component. + Performs a search for a contact by its partial last name. } function TVpContacts.FindContactByName(const AName: string; - CaseInsensitive: Boolean): TVpContact; + CaseInsensitive: Boolean = true): TVpContact; var I: Integer; SearchStr: String; @@ -3018,7 +3019,48 @@ begin end; end; -{ new function introduced to support the new buttonbar component } +{ This overload of FindContactByName performs another search for a contact, + but now on complete first and last names. } +function TVpContacts.FindContactByName(const AFirstName, ALastName: string; + CaseInsensitive: Boolean = true): TVpContact; +var + I: Integer; + SearchStr1, SearchStr2: String; + SearchName1, SearchName2: String; + contact: TVpContact; +begin + Result := nil; + + // To enhance performance, uppercase the input name and get its length only once + if CaseInsensitive then + begin + SearchStr1 := UpperCase(AFirstName); + SearchStr2 := UpperCase(ALastName); + end else + begin + SearchStr1 := AFirstName; + SearchStr2 := ALastName; + end; + + // Iterate the contacts looking for a match + for I := 0 to FContactsList.Count - 1 do begin + contact := TVpContact(FContactsList[I]); + SearchName1 := contact.FirstName; + SearchName2 := contact.LastName; + if CaseInsensitive then + begin + SearchName1 := Uppercase(SearchName1); + SearchName2 := Uppercase(SearchName2); + end; + // We found a match, so return it and bail out + if (SearchName1 = SearchStr1) and (SearchName2 = SearchStr2) then begin + Result := TVpContact(FContactsList[I]); + exit; + end; + end; +end; + +{ new function introduced to support the new buttonbar component } function TVpContacts.FindContactIndexByName(const Name: string; CaseInsensitive: Boolean): Integer; var