lcl: handle NullValueKey of TDBLookup* in KeyDown event instead of through a global handler. Fixes #19164

git-svn-id: trunk@30329 -
This commit is contained in:
blikblum 2011-04-17 01:26:18 +00:00
parent 4e77a0efec
commit 3cbd34d275
4 changed files with 38 additions and 39 deletions

View File

@ -145,7 +145,6 @@ Type
FLookupCache: boolean; FLookupCache: boolean;
FLookupList: TLookupList; FLookupList: TLookupList;
FNullValueKey: TShortcut; FNullValueKey: TShortcut;
FOnClearSelection: TNotifyEvent;
procedure ActiveChange(Sender: TObject); procedure ActiveChange(Sender: TObject);
procedure EditingChange(Sender: TObject); procedure EditingChange(Sender: TObject);
procedure FetchLookupData; procedure FetchLookupData;
@ -157,11 +156,10 @@ 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); function HandleNullKey(var Key: Word; Shift: TShiftState): Boolean;
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;
@ -178,7 +176,6 @@ Type
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 NullValueKey: TShortcut read FNullValueKey write FNullValueKey;
property OnClearSelection: TNotifyEvent read FOnClearSelection write FOnClearSelection;
end; end;
{ TDBEdit } { TDBEdit }
@ -454,10 +451,10 @@ Type
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); procedure SetNullValueKey(const AValue: TShortCut);
protected protected
procedure DataChange(Sender: TObject); override; procedure DataChange(Sender: TObject); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure InitializeWnd; override; procedure InitializeWnd; override;
procedure UpdateData(Sender: TObject); override; procedure UpdateData(Sender: TObject); override;
public public
@ -769,9 +766,9 @@ Type
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 SetNullValueKey(const AValue: TShortCut);
procedure RemoveSelection(Sender: TObject);
protected protected
procedure InitializeWnd; override; procedure InitializeWnd; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure UpdateData(Sender: TObject); override; procedure UpdateData(Sender: TObject); override;
procedure UpdateText; override; procedure UpdateText; override;
public public

View File

@ -75,7 +75,6 @@ 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;
@ -147,12 +146,17 @@ begin
FLookupList := TLookupList.Create; FLookupList := TLookupList.Create;
end; end;
procedure TDBLookup.NullKeyHandler(Sender: TObject; var Key: Word; {
Shift: TShiftState); Returns True if selection must be cleared
If NullKey is detected set Key to VK_UNKNOWN
}
function TDBLookup.HandleNullKey(var Key: Word; Shift: TShiftState): Boolean;
var var
i: Integer; i: Integer;
begin begin
if (FNullValueKey=KeyToShortCut(Key, Shift)) then Result:=False;
if FNullValueKey=KeyToShortCut(Key, Shift) then
begin begin
// null associated field // null associated field
if Assigned(FControlLink) and (FDataFieldNames<>'') and FControlLink.Active then if Assigned(FControlLink) and (FDataFieldNames<>'') and FControlLink.Active then
@ -161,11 +165,9 @@ begin
for i:=0 to FDataFields.Count-1 do for i:=0 to FDataFields.Count-1 do
TField(FDataFields[i]).Clear; TField(FDataFields[i]).Clear;
end else end else
if Assigned(FListLink.DataSet) and FListLink.DataSet.Active and Assigned(FListField) then Result:=Assigned(FListLink.DataSet) and FListLink.DataSet.Active and Assigned(FListField);
// associated list is active, clear current selection Key:=VK_UNKNOWN;
DoClearSelection;
end; end;
end; end;
procedure TDBLookup.Notification(AComponent: TComponent; Operation: TOperation); procedure TDBLookup.Notification(AComponent: TComponent; Operation: TOperation);
@ -176,12 +178,6 @@ 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,7 +27,6 @@ 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;
@ -55,6 +54,17 @@ begin
ActiveChange(Self); ActiveChange(Self);
end; end;
procedure TDBLookupComboBox.KeyDown(var Key: Word; Shift: TShiftState);
begin
if FLookup.HandleNullKey(Key, Shift) then
begin
//clear selection
ItemIndex := -1;
//Text := '';
end;
inherited KeyDown(Key, Shift);
end;
function TDBLookupComboBox.GetKeyField: string; function TDBLookupComboBox.GetKeyField: string;
begin begin
Result := FLookup.KeyField; Result := FLookup.KeyField;
@ -133,10 +143,3 @@ begin
FLookup.NullValueKey := AValue; FLookup.NullValueKey := AValue;
end; end;
procedure TDBLookupComboBox.RemoveSelection(Sender: TObject);
begin
ItemIndex := -1;
Text := '';
end;

View File

@ -26,7 +26,6 @@ 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;
@ -59,6 +58,19 @@ begin
end; end;
end; end;
procedure TDBLookupListBox.KeyDown(var Key: Word; Shift: TShiftState);
begin
if FLookup.HandleNullKey(Key, Shift) then
begin
//clear selection
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;
inherited KeyDown(Key, Shift);
end;
procedure TDBLookupListBox.InitializeWnd; procedure TDBLookupListBox.InitializeWnd;
begin begin
inherited InitializeWnd; inherited InitializeWnd;
@ -141,15 +153,6 @@ 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); procedure TDBLookupListBox.SetNullValueKey(const AValue: TShortCut);
begin begin
FLookup.NullValueKey := AValue; FLookup.NullValueKey := AValue;