From a43056f29196af182f8becb3517f98afefa53d32 Mon Sep 17 00:00:00 2001 From: juha Date: Sat, 4 Apr 2020 12:51:38 +0000 Subject: [PATCH] LCL: "EmptyValue" and "DisplayEmpty" properties for TBLookupCombobox and TDBLookupListBox components. Issue #36035, patch from Zdravko Gabrovski. git-svn-id: trunk@62884 - --- lcl/dbctrls.pp | 16 ++++++++++++++++ lcl/include/dblookup.inc | 30 ++++++++++++++++++++++++++++-- lcl/include/dblookupcombobox.inc | 24 ++++++++++++++++++++++++ lcl/include/dblookuplistbox.inc | 24 ++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/lcl/dbctrls.pp b/lcl/dbctrls.pp index a3c4b729bc..5d8fe813ae 100644 --- a/lcl/dbctrls.pp +++ b/lcl/dbctrls.pp @@ -123,6 +123,8 @@ Type FDataFieldNames: string; FKeyFieldNames: string; FListFieldName: string; + FEmptyValue: string; + FDisplayEmpty: string; FListFieldIndex: Integer; FDataFields: TList; // Data Fields to lookup/edit FKeyFields: TList; // Keyfields in lookup dataset @@ -169,6 +171,8 @@ Type property ListSource: TDataSource read GetListSource write SetListSource; property NullValueKey: TShortcut read FNullValueKey write FNullValueKey; property ScrollListDataset: Boolean read FScrollListDataset write FScrollListDataset; + property EmptyValue : String read FEmptyValue write FEmptyVAlue; + property DisplayEmpty : String read FDisplayEmpty write FDisplayEmpty; end; { TDBEdit } @@ -451,6 +455,8 @@ Type private FLookup: TDBLookup; procedure ActiveChange(Sender: TObject); + function GetDisplayEmpty: String; + function GetEmptyValue: String; function GetKeyField: string; function GetKeyValue: Variant; function GetListField: string; @@ -459,6 +465,8 @@ Type function GetLookupCache: boolean; function GetNullValueKey: TShortCut; function GetScrollListDataset: Boolean; + procedure SetDisplayEmpty(AValue: String); + procedure SetEmptyValue(AValue: String); procedure SetKeyField(const Value: string); procedure SetKeyValue(const AValue: Variant); procedure SetListField(const Value: string); @@ -504,6 +512,8 @@ Type property ListSource: TDataSource read GetListSource write SetListSource; property LookupCache: boolean read GetLookupCache write SetLookupCache; property NullValueKey: TShortCut read GetNullValueKey write SetNullValueKey default 0; + property EmptyValue: String read GetEmptyValue write SetEmptyValue; + property DisplayEmpty: String read GetDisplayEmpty write SetDisplayEmpty; // property MultiSelect; property OnClick; property OnDblClick; @@ -852,6 +862,8 @@ Type private FLookup: TDBLookup; procedure ActiveChange(Sender: TObject); + function GetDisplayEmpty: String; + function GetEmptyValue: String; function GetKeyField: string; function GetKeyValue: variant; function GetListField: string; @@ -860,6 +872,8 @@ Type function GetLookupCache: boolean; function GetNullValueKey: TShortCut; function GetScrollListDataset: Boolean; + procedure SetDisplayEmpty(AValue: String); + procedure SetEmptyValue(AValue: String); procedure SetKeyField(const Value: string); procedure SetKeyValue(const AValue: variant); procedure SetListField(const Value: string); @@ -915,6 +929,8 @@ Type property LookupCache: boolean read GetLookupCache write SetLookupCache; // property MaxLength default -1; property NullValueKey: TShortCut read GetNullValueKey write SetNullValueKey default 0; + property EmptyValue: String read GetEmptyValue write SetEmptyValue; + property DisplayEmpty: String read GetDisplayEmpty write SetDisplayEmpty; property OnChange; property OnChangeBounds; property OnClick; diff --git a/lcl/include/dblookup.inc b/lcl/include/dblookup.inc index c00e6515b0..44985d2dc2 100644 --- a/lcl/include/dblookup.inc +++ b/lcl/include/dblookup.inc @@ -106,6 +106,8 @@ begin FDataFields := TList.Create; FKeyFields := TList.Create; FListLink := TDBLookupDataLink.Create(Self); + FDisplayEmpty := ''; + FEmptyValue := ''; //FHasLookUpField := False; //FLookupCache := False; end; @@ -281,8 +283,22 @@ begin ListLinkDataSet := FListLink.DataSet; if not (Assigned(ListLinkDataSet) and Assigned(FListField)) then Exit; - if ListLinkDataSet.IsEmpty then + + if ListLinkDataSet.IsEmpty then begin + // Add Empty value if no recs into dataset + if FEmptyValue<>'' then begin + FControlItems.BeginUpdate; + try + KeyIndex := FControlItems.Add(FDisplayEmpty); + SetLength(FListKeys, 1); + FListKeys[KeyIndex] := FEmptyValue; + KeyListCount := 1; + finally + FControlItems.EndUpdate; + end; + end; Exit; + end; Bookmark := ListLinkDataSet.GetBookmark; //in fpc 2.6.4, TMemDataset does not supports BlockRead. Issues 26356, 27959 {$IF FPC_FULLVERSION < 30000} @@ -302,8 +318,18 @@ begin //needed to handle sqldb.TSQLQuery that does not has a reliable recordcount after Open ListLinkDataSet.Last; ListLinkDataSet.First; - SetLength(FListKeys, ListLinkDataSet.RecordCount); KeyListCount := 0; + SetLength(FListKeys, ListLinkDataSet.RecordCount); + // Handle Empty Value and Empty Display + if FEmptyValue<>'' then begin + KeyIndex := FControlItems.Add(FDisplayEmpty); + if KeyIndex<>length(FListKeys) then // sanity check + raise Exception.Create('TDBLookup.FetchLookupData: inconsistency'); + SetLength(FListKeys, KeyIndex+1); // Add one more + FListKeys[KeyIndex] := FEmptyValue; + KeyListCount := 1; + end; + while not ListLinkDataSet.EOF do begin KeyIndex := FControlItems.Add(FListField.DisplayText); diff --git a/lcl/include/dblookupcombobox.inc b/lcl/include/dblookupcombobox.inc index ef53390b3b..8d5e0ea0c3 100644 --- a/lcl/include/dblookupcombobox.inc +++ b/lcl/include/dblookupcombobox.inc @@ -21,6 +21,8 @@ constructor TDBLookupComboBox.Create(AOwner: TComponent); begin inherited Create(AOwner); FLookup := TDBLookup.Create(Self); + EmptyValue := ''; + DisplayEmpty := ''; FDataLink.OnActiveChange := @ActiveChange; end; @@ -68,6 +70,16 @@ begin UpdateLookup; end; +function TDBLookupComboBox.GetDisplayEmpty: String; +begin + Result := FLookup.DisplayEmpty; +end; + +function TDBLookupComboBox.GetEmptyValue: string; +begin + Result := FLookup.EmptyValue; +end; + procedure TDBLookupComboBox.DataChange(Sender: TObject); begin UpdateItemIndex; @@ -153,6 +165,18 @@ begin result := FLookup.NullValueKey; end; +procedure TDBLookupComboBox.SetDisplayEmpty(AValue: String); +begin + FLookup.DisplayEmpty:=AValue; + UpdateLookup; +end; + +procedure TDBLookupComboBox.SetEmptyValue(AValue: string); +begin + FLookup.EmptyValue:=AValue; + UpdateLookup; +end; + function TDBLookupComboBox.GetScrollListDataset: Boolean; begin Result := FLookup.ScrollListDataset; diff --git a/lcl/include/dblookuplistbox.inc b/lcl/include/dblookuplistbox.inc index e1d4b64b1e..73871f2633 100644 --- a/lcl/include/dblookuplistbox.inc +++ b/lcl/include/dblookuplistbox.inc @@ -20,6 +20,8 @@ constructor TDBLookupListBox.Create(AOwner: TComponent); begin inherited Create(AOwner); FLookup:= TDBLookup.Create(Self); + EmptyValue := ''; + DisplayEmpty := ''; FDataLink.OnActiveChange:= @ActiveChange; end; @@ -41,6 +43,16 @@ begin UpdateLookup; end; +function TDBLookupListBox.GetDisplayEmpty: String; +begin + Result := FLookup.DisplayEmpty; +end; + +function TDBLookupListBox.GetEmptyValue: string; +begin + Result := FLookup.EmptyValue; +end; + procedure TDBLookupListBox.DataChange(Sender: TObject); begin if FDatalink.Active then @@ -134,6 +146,18 @@ begin Result := FLookup.NullValueKey; end; +procedure TDBLookupListBox.SetDisplayEmpty(AValue: String); +begin + FLookup.DisplayEmpty := AValue; + UpdateLookup; +end; + +procedure TDBLookupListBox.SetEmptyValue(AValue: String); +begin + FLookup.EmptyValue := AValue; + UpdateLookup; +end; + function TDBLookupListBox.GetScrollListDataset: Boolean; begin Result := FLookup.ScrollListDataset;