LCL, implements NullValueKey for dblookuplistbox and dblookupcombobox, issue #14408

git-svn-id: trunk@28158 -
This commit is contained in:
jesus 2010-11-08 20:44:44 +00:00
parent db0a5c00b2
commit d112e09512
4 changed files with 79 additions and 0 deletions

View File

@ -143,6 +143,8 @@ Type
FListField: TField; // Result field in lookup dataset FListField: TField; // Result field in lookup dataset
FLookupCache: boolean; FLookupCache: boolean;
FLookupList: TLookupList; FLookupList: TLookupList;
FNullValueKey: TShortcut;
FOnClearSelection: TNotifyEvent;
procedure ActiveChange(Sender: TObject); procedure ActiveChange(Sender: TObject);
procedure EditingChange(Sender: TObject); procedure EditingChange(Sender: TObject);
procedure FetchLookupData; procedure FetchLookupData;
@ -154,9 +156,11 @@ Type
procedure SetListFieldName(const Value: string); procedure SetListFieldName(const Value: string);
procedure SetListSource(Value: TDataSource); procedure SetListSource(Value: TDataSource);
procedure SetLookupCache(const Value: boolean); procedure SetLookupCache(const Value: boolean);
procedure NullKeyHandler(Sender: TObject; var Key: Word; Shift: TShiftState);
protected protected
procedure Notification(AComponent: TComponent; procedure Notification(AComponent: TComponent;
Operation: TOperation); override; Operation: TOperation); override;
procedure DoClearSelection;
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
@ -172,6 +176,8 @@ Type
property ListField: string read FListFieldName write SetListFieldName; property ListField: string read FListFieldName write SetListFieldName;
property ListFieldIndex: Integer read FListFieldIndex write FListFieldIndex default 0; property ListFieldIndex: Integer read FListFieldIndex write FListFieldIndex default 0;
property ListSource: TDataSource read GetListSource write SetListSource; property ListSource: TDataSource read GetListSource write SetListSource;
property NullValueKey: TShortcut read FNullValueKey write FNullValueKey;
property OnClearSelection: TNotifyEvent read FOnClearSelection write FOnClearSelection;
end; end;
{ TDBEdit } { TDBEdit }
@ -444,12 +450,15 @@ Type
function GetListFieldIndex: Integer; function GetListFieldIndex: Integer;
function GetListSource: TDataSource; function GetListSource: TDataSource;
function GetLookupCache: boolean; function GetLookupCache: boolean;
function GetNullValueKey: TShortCut;
procedure SetKeyField(const Value: string); procedure SetKeyField(const Value: string);
procedure SetKeyValue(const AValue: Variant); procedure SetKeyValue(const AValue: Variant);
procedure SetListField(const Value: string); procedure SetListField(const Value: string);
procedure SetListFieldIndex(const Value: Integer); procedure SetListFieldIndex(const Value: Integer);
procedure SetListSource(const Value: TDataSource); procedure SetListSource(const Value: TDataSource);
procedure SetLookupCache(const Value: boolean); procedure SetLookupCache(const Value: boolean);
procedure RemoveSelection(Sender: TObject);
procedure SetNullValueKey(const AValue: TShortCut);
protected protected
procedure DataChange(Sender: TObject); override; procedure DataChange(Sender: TObject); override;
procedure InitializeWnd; override; procedure InitializeWnd; override;
@ -473,6 +482,7 @@ Type
property ListFieldIndex: Integer read GetListFieldIndex write SetListFieldIndex; property ListFieldIndex: Integer read GetListFieldIndex write SetListFieldIndex;
property ListSource: TDataSource read GetListSource write SetListSource; property ListSource: TDataSource read GetListSource write SetListSource;
property LookupCache: boolean read GetLookupCache write SetLookupCache; property LookupCache: boolean read GetLookupCache write SetLookupCache;
property NullValueKey: TShortCut read GetNullValueKey write SetNullValueKey default 0;
// property MultiSelect; // property MultiSelect;
property OnClick; property OnClick;
property OnDblClick; property OnDblClick;
@ -755,12 +765,15 @@ Type
function GetListFieldIndex: Integer; function GetListFieldIndex: Integer;
function GetListSource: TDataSource; function GetListSource: TDataSource;
function GetLookupCache: boolean; function GetLookupCache: boolean;
function GetNullValueKey: TShortCut;
procedure SetKeyField(const Value: string); procedure SetKeyField(const Value: string);
procedure SetKeyValue(const AValue: variant); procedure SetKeyValue(const AValue: variant);
procedure SetListField(const Value: string); procedure SetListField(const Value: string);
procedure SetListFieldIndex(const Value: Integer); procedure SetListFieldIndex(const Value: Integer);
procedure SetListSource(const Value: TDataSource); procedure SetListSource(const Value: TDataSource);
procedure SetLookupCache(const Value: boolean); procedure SetLookupCache(const Value: boolean);
procedure SetNullValueKey(const AValue: TShortCut);
procedure RemoveSelection(Sender: TObject);
protected protected
procedure InitializeWnd; override; procedure InitializeWnd; override;
procedure UpdateData(Sender: TObject); override; procedure UpdateData(Sender: TObject); override;
@ -792,6 +805,7 @@ Type
property ListSource: TDataSource read GetListSource write SetListSource; property ListSource: TDataSource read GetListSource write SetListSource;
property LookupCache: boolean read GetLookupCache write SetLookupCache; property LookupCache: boolean read GetLookupCache write SetLookupCache;
// property MaxLength default -1; // property MaxLength default -1;
property NullValueKey: TShortCut read GetNullValueKey write SetNullValueKey default 0;
property OnChange; property OnChange;
property OnChangeBounds; property OnChangeBounds;
property OnClick; property OnClick;

View File

@ -75,6 +75,7 @@ begin
FHasLookUpField:= False; FHasLookUpField:= False;
FListLinkTmpSetActive := False; FListLinkTmpSetActive := False;
FLookupCache := False; FLookupCache := False;
Application.AddOnKeyDownBeforeHandler(@NullKeyHandler, false);
end; end;
destructor TDBLookup.Destroy; destructor TDBLookup.Destroy;
@ -146,6 +147,27 @@ begin
FLookupList := TLookupList.Create; FLookupList := TLookupList.Create;
end; end;
procedure TDBLookup.NullKeyHandler(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i: Integer;
begin
if (FNullValueKey=KeyToShortCut(Key, Shift)) then
begin
// null associated field
if Assigned(FControlLink) and (FDataFieldNames<>'') and FControlLink.Active then
begin
FControlLink.DataSet.Edit;
for i:=0 to FDataFields.Count-1 do
TField(FDataFields[i]).Clear;
end else
if Assigned(FListLink.DataSet) and FListLink.DataSet.Active and Assigned(FListField) then
// associated list is active, clear current selection
DoClearSelection;
end;
end;
procedure TDBLookup.Notification(AComponent: TComponent; Operation: TOperation); procedure TDBLookup.Notification(AComponent: TComponent; Operation: TOperation);
begin begin
inherited Notification(AComponent, Operation); inherited Notification(AComponent, Operation);
@ -154,6 +176,12 @@ begin
FListSource:= nil; FListSource:= nil;
end; end;
procedure TDBLookup.DoClearSelection;
begin
if Assigned(FOnClearSelection) then
OnClearSelection(Self);
end;
procedure TDBLookup.LinkGetBookMark; procedure TDBLookup.LinkGetBookMark;
begin begin
FListLinkTmpSetActive := not FListLink.DataSet.Active; FListLinkTmpSetActive := not FListLink.DataSet.Active;

View File

@ -27,6 +27,7 @@ constructor TDBLookupComboBox.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FLookup:= TDBLookup.Create(Self); FLookup:= TDBLookup.Create(Self);
FLookup.OnClearSelection:=@RemoveSelection;
FDataLink.OnActiveChange:= @ActiveChange; FDataLink.OnActiveChange:= @ActiveChange;
end; end;
@ -84,6 +85,11 @@ begin
Result := FLookup.LookupCache; Result := FLookup.LookupCache;
end; end;
function TDBLookupComboBox.GetNullValueKey: TShortCut;
begin
result := FLookup.NullValueKey;
end;
procedure TDBLookupComboBox.SetKeyField(const Value: string); procedure TDBLookupComboBox.SetKeyField(const Value: string);
begin begin
FLookup.KeyField:= Value; FLookup.KeyField:= Value;
@ -122,4 +128,15 @@ begin
ActiveChange(Self); ActiveChange(Self);
end; end;
procedure TDBLookupComboBox.SetNullValueKey(const AValue: TShortCut);
begin
FLookup.NullValueKey := AValue;
end;
procedure TDBLookupComboBox.RemoveSelection(Sender: TObject);
begin
ItemIndex := -1;
Text := '';
end;

View File

@ -26,6 +26,7 @@ constructor TDBLookupListBox.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FLookup:= TDBLookup.Create(Self); FLookup:= TDBLookup.Create(Self);
FLookup.OnClearSelection := @RemoveSelection;
FDataLink.OnActiveChange:= @ActiveChange; FDataLink.OnActiveChange:= @ActiveChange;
end; end;
@ -97,6 +98,11 @@ begin
Result := FLookup.LookupCache; Result := FLookup.LookupCache;
end; end;
function TDBLookupListBox.GetNullValueKey: TShortCut;
begin
Result := FLookup.NullValueKey;
end;
procedure TDBLookupListBox.SetKeyField(const Value: string); procedure TDBLookupListBox.SetKeyField(const Value: string);
begin begin
FLookup.KeyField:= Value; FLookup.KeyField:= Value;
@ -135,3 +141,17 @@ begin
ActiveChange(Self); ActiveChange(Self);
end; end;
procedure TDBLookupListBox.RemoveSelection(Sender: TObject);
begin
if not assigned(FDatalink.DataSet) or not FDatalink.DataSet.Active or
not assigned(FDatalink.Field) then
GetItemIndex; // a list-only dblookuplist do not automatically update itemindex
ItemIndex := -1;
end;
procedure TDBLookupListBox.SetNullValueKey(const AValue: TShortCut);
begin
FLookup.NullValueKey := AValue;
end;