JvclLaz: Add OnSearchRecord event to TJvDBSearchEdit.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9354 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2024-05-28 21:58:26 +00:00
parent c335a9e65a
commit 0e9a6e81b3

View File

@ -38,6 +38,9 @@ uses
Classes, Controls, StdCtrls, DB, DBCtrls;
type
TJvSearchRecordEvent = procedure (ADataset: TDataset; KeyField, KeyValue: string;
Options: TLocateOptions; var Found: Boolean) of object;
TJvDBCustomSearchEdit = class(TCustomEdit) //JvCustomEdit)
private
FDataLink: TFieldDataLink;
@ -45,6 +48,7 @@ type
FClearOnEnter: Boolean;
FDataResult: string;
FRaiseLocateException: Boolean;
FOnSearchRecord: TJvSearchRecordEvent;
procedure DataChange(Sender: TObject);
function GetDataSource: TDataSource;
function GetDataField: string;
@ -55,6 +59,7 @@ type
protected
procedure DoEnter; override;
procedure DoExit; override;
function DoSearchRecord(KeyField, KeyValue: String; Options: TLocateOptions): Boolean;
procedure KeyPress(var Key: Char); override;
procedure Notification(Component: TComponent; Operation: TOperation); override;
public
@ -71,6 +76,7 @@ type
property ClearOnEnter: Boolean read FClearOnEnter write FClearOnEnter default True;
//1 Property to raise/hide any exception inside the Dataset.Locate call
property RaiseLocateException: Boolean read FRaiseLocateException write FRaiseLocateException default true;
property OnSearchRecord: TJvSearchRecordEvent read FOnSearchRecord write FOnSearchRecord;
end;
{$IFDEF RTL230_UP}
@ -213,16 +219,17 @@ end;
procedure TJvDBCustomSearchEdit.CMChanged(var Msg: TLMessage);
var
LText: string;
lText: string;
begin
if (not ((csDesigning in ComponentState) and
(csLoading in ComponentState))) and
Assigned(FDataLink.DataSet) then
if (Screen.ActiveControl = Self) and FDataLink.Active then
try
if FDataLink.DataSet.Locate(FDataLink.FieldName, Text, FSearchOptions) then
lText := Text;
if DoSearchRecord(FDataLink.FieldName, lText, FSearchOptions) then
begin
LText := Text;
lText := Text;
Text := FDataLink.DataSet.FieldByName(DataField).AsString;
SelStart := Length(LText);
SelLength := Length(Text) - SelStart;
@ -293,6 +300,28 @@ begin
Text := FDataLink.DataSet.FieldByName(DataField).AsString;
end;
function TJvDBCustomSearchEdit.DoSearchRecord(KeyField, KeyValue: String;
Options: TLocateOptions): Boolean;
var
bm: TBookmark;
begin
if Assigned(FOnSearchRecord) then
begin
bm := FDatalink.Dataset.GetBookmark;
FDataLink.Dataset.DisableControls;
try
FOnSearchRecord(FDataLink.Dataset, KeyField, KeyValue, Options, Result);
if not Result then
FDataLink.Dataset.GotoBookmark(bm);
finally
FDatalink.Dataset.EnableControls;
FDatalink.Dataset.FreeBookmark(bm);
end;
end
else
Result := FDataLink.Dataset.Locate(Keyfield, KeyValue, Options);
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);