使非窗口化组件数据感知



我有一个带有日期属性的非窗口组件。我想通过对日期字段的读写功能使此组件数据感知。(换句话说,如果我在运行时更改日期,我想将新的日期属性值写入数据集。我用谷歌搜索了示例,但我找不到任何示例。找到了几个只读示例,例如TDbLabel,但没有一个允许将更改写入数据集的示例。如果有人能给我举个例子,我将不胜感激。

这是解决您问题的通用代码方法

unit Unit1;
interface
uses
   Classes
  ,DB
  ,DBCtrls
  ;
type
  TDataAwareComponent = class(TComponent)
  private
    { private declarations }
    FDataLink : TFieldDataLink;
    FFromDateChange : Boolean;
    function GetDataField: string;
    function GetDataSource: TDataSource;
    function GetField: TField;
    function GetReadOnly: Boolean;
    procedure SetDataField(const Value: string);
    procedure SetDataSource(const Value: TDataSource);
    procedure SetReadOnly(const Value: Boolean);
    function GetDateProperty: TDateTime;
    procedure SetDateProperty(const Value: TDateTime);
  protected
    { protected declarations }
    procedure DataChange(Sender : TObject);
  public
    { public declarations }
    constructor Create(AOwner : TComponent);override;
    destructor  Destroy;override;
  public
    property Field        : TField    read GetField;
    property DateProperty : TDateTime read GetDateProperty write SetDateProperty;
  published
    { published declarations }
   property DataSource   : TDataSource           read GetDataSource  write SetDataSource;
   property DataField    : string                read GetDataField   write SetDataField;
   property ReadOnly     : Boolean               read GetReadOnly    write SetReadOnly;
  end;
implementation
{ TDataAwareComponent }
{------------------------------------------------------------------------------}
constructor TDataAwareComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDataLink                 := TFieldDataLink.Create;
  FDataLink.Control         := Self;
  FDataLink.OnDataChange    := DataChange;
end;
{------------------------------------------------------------------------------}
destructor TDataAwareComponent.Destroy;
begin
  FDataLink.Free;
  FDataLink := nil;
  inherited;
end;
{------------------------------------------------------------------------------}
procedure TDataAwareComponent.DataChange(Sender: TObject);
begin
  // Here a visual object would display the underlying field value.
  // For example if this component was a TEdit
  // the code would be something like
{  if FDataLink.Active then
    Self.Text := Field.AsString;
    And on the exit event of the TEdit the code would be reversed
    so the field can take the value the user entered.
    Since you do not need any UI interaction propably this event
    is useless to you.
    }
    // Of course there are more issues you should cover to
    // have a complete working solution.
    // This is only demostration code.
end;
{------------------------------------------------------------------------------}
function TDataAwareComponent.GetDataField: string;
begin
  Result := FDataLink.FieldName
end;
{------------------------------------------------------------------------------}
function TDataAwareComponent.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;
{------------------------------------------------------------------------------}
function TDataAwareComponent.GetDateProperty: TDateTime;
begin
  //You do not need a separate property since you can access directly the field value.
  Result := 0;
  if Assigned(Field) and (Field.DataType in [ftTime,ftDate,ftDateTime]   then
    Result := Field.AsDateTime;
end;
{------------------------------------------------------------------------------}
function TDataAwareComponent.GetField: TField;
begin
  Result : FDataLink.Field;
end;
{------------------------------------------------------------------------------}
function TDataAwareComponent.GetReadOnly: Boolean;
begin
  Result := FDataLink.ReadOnly;
end;
{------------------------------------------------------------------------------}
procedure TDataAwareComponent.SetDataField(const Value: string);
begin
  FDataLink.FieldName := Value;
end;
{------------------------------------------------------------------------------}
procedure TDataAwareComponent.SetDataSource(const Value: TDataSource);
begin
  FDataLink.DataSource := Value;
end;
{------------------------------------------------------------------------------}
procedure TDataAwareComponent.SetDateProperty(const Value: TDateTime);
begin
  if Assigned(Field)  then
  begin
    FFromDateChange := True;
    try
      Field.DataSet.Edit;
      Field.Value := Value;
    finally
      FFromDateChange := False;
    end;
  end;
end;
{------------------------------------------------------------------------------}
procedure TDataAwareComponent.SetReadOnly(const Value: Boolean);
begin
  FDataLink.ReadOnly := Value;
end;
{------------------------------------------------------------------------------}
end.

这将是一些代码,但是如果您的组件在其值更改时触发了事件,那么如果数据集处于dsEdit状态,则可以将其用于将更新的值发送到数据集。 您还需要处理各种 TDataSource 事件,以了解数据集何时更改。

相关内容

  • 没有找到相关文章

最新更新