LCL, implements KeyValue for DbLookupComboBox and DbLookupListbox components, issue #16557

git-svn-id: trunk@28031 -
This commit is contained in:
jesus 2010-11-02 00:50:53 +00:00
parent 2e33884219
commit 8f1ae24251
5 changed files with 103 additions and 0 deletions

View File

@ -163,6 +163,8 @@ Type
procedure Initialize(AControlDataLink: TFieldDataLink; AControlItems: TStrings);
function ListFieldValue: string;
procedure UpdateData(const AListFieldValue: string);
function GetKeyValue(const AListFieldValue: string): Variant;
function GetListValue(const AKeyValue: Variant; out ItemIndex:Integer): boolean;
property LookupCache: boolean read FLookupCache write SetLookupCache;
// properties to be published by owner control
// these are not used where data control Field is dbLookup
@ -437,11 +439,13 @@ Type
FLookup: TDBLookup;
procedure ActiveChange(Sender: TObject);
function GetKeyField: string;
function GetKeyValue: Variant;
function GetListField: string;
function GetListFieldIndex: Integer;
function GetListSource: TDataSource;
function GetLookupCache: boolean;
procedure SetKeyField(const Value: string);
procedure SetKeyValue(const AValue: Variant);
procedure SetListField(const Value: string);
procedure SetListFieldIndex(const Value: Integer);
procedure SetListSource(const Value: TDataSource);
@ -452,6 +456,7 @@ Type
procedure UpdateData(Sender: TObject); override;
public
constructor Create(AOwner: TComponent); override;
property KeyValue: Variant read GetKeyValue write SetKeyValue;
published
property Align;
property Anchors;
@ -745,11 +750,13 @@ Type
FLookup: TDBLookup;
procedure ActiveChange(Sender: TObject);
function GetKeyField: string;
function GetKeyValue: variant;
function GetListField: string;
function GetListFieldIndex: Integer;
function GetListSource: TDataSource;
function GetLookupCache: boolean;
procedure SetKeyField(const Value: string);
procedure SetKeyValue(const AValue: variant);
procedure SetListField(const Value: string);
procedure SetListFieldIndex(const Value: Integer);
procedure SetListSource(const Value: TDataSource);
@ -760,6 +767,7 @@ Type
procedure UpdateText; override;
public
constructor Create(AOwner: TComponent); override;
property KeyValue: variant read GetKeyValue write SetKeyValue;
published
property Align;
property Anchors;

View File

@ -545,6 +545,8 @@ end;
procedure TCustomListBox.SetItemIndex(AIndex: integer);
begin
if AIndex=FItemIndex then
exit;
if (AIndex >= FItems.Count) then
RaiseIndexOutOfBounds(AIndex);
if AIndex < 0 then AIndex := -1;

View File

@ -373,4 +373,68 @@ begin
LinkGotoBookMark;
end;
end;
function TDBLookup.GetKeyValue(const AListFieldValue: string): Variant;
begin
// Check first in LookupCache if enabled
if FLookupCache and not FLookupFieldIsCached then
begin
Result := FLookupList.FirstKeyByValue(AListFieldValue);
Exit;
end;
Result := NULL;
if not (Assigned(FListLink.DataSet) and Assigned(FListField) and
(FKeyFields.Count>0)) then
exit;
if FListField.AsString=AListFieldValue then
begin
result := TField(FKeyFields[0]).Value;
exit;
end;
LinkGetBookmark;
try
if FListLink.DataSet.Locate(FListFieldName, AListFieldValue, []) then
result := TField(FKeyFields[0]).Value;
finally
LinkGotoBookmark;
end;
end;
function TDBLookup.GetListValue(const AKeyValue: Variant; out ItemIndex:Integer
): boolean;
var
AValue: Variant;
begin
// Check first in LookupCache if enabled
if FLookupCache and not FLookupFieldIsCached then
AValue := FLookupList.ValueOfKey(AKeyValue)
else begin
AValue := Null;
if Assigned(FListlink.Dataset) and Assigned(FListField) and
(FKeyFields.Count>0) then
begin
if TField(FKeyFields[0]).Value=AKeyValue then
AValue := FListField.Value
else
begin
LinkGetBookmark;
try
if FListLink.DataSet.Locate(FKeyFieldNames, AKeyValue, []) then
AValue := FListField.Value;
finally
LinkGotoBookmark;
end;
end;
end;
end;
result := not VarIsNull(AValue);
if Result then
ItemIndex := FControlItems.IndexOf(AValue);
end;
{$ENDIF}

View File

@ -59,6 +59,11 @@ begin
Result := FLookup.KeyField;
end;
function TDBLookupComboBox.GetKeyValue: variant;
begin
result := FLookup.GetKeyValue(Text);
end;
function TDBLookupComboBox.GetListField: string;
begin
Result := FLookup.ListField;
@ -85,6 +90,14 @@ begin
ActiveChange(Self);
end;
procedure TDBLookupComboBox.SetKeyValue(const AValue: variant);
var
AItemIndex: Integer;
begin
if FLookup.GetListValue(AValue, AItemIndex) then
ItemIndex := AItemIndex;
end;
procedure TDBLookupComboBox.SetListField(const Value: string);
begin
FLookup.ListField:= Value;

View File

@ -60,6 +60,14 @@ begin
Result := FLookup.KeyField;
end;
function TDBLookupListBox.GetKeyValue: Variant;
begin
if ItemIndex<0 then
result := NULL
else
result := FLookup.GetKeyValue(Items[ItemIndex]);
end;
function TDBLookupListBox.GetListField: string;
begin
Result := FLookup.ListField;
@ -86,6 +94,14 @@ begin
ActiveChange(Self);
end;
procedure TDBLookupListBox.SetKeyValue(const AValue: Variant);
var
AItemIndex: Integer;
begin
if FLookup.GetListValue(AValue, AItemIndex) then
ItemIndex := AItemIndex;
end;
procedure TDBLookupListBox.SetListField(const Value: string);
begin
FLookup.ListField:= Value;