我正在尝试在运行时以表单形式定义ClientDataSet组件。我可以在VCL表单程序中成功定义所有字段并操作ClientDataSet,但是,当我尝试将诸如AfterInsert之类的事件的事件处理程序添加到代码中时,编译器对象将变为我的格式。

clientDataset是在以下过程中创建的:

procedure TForm1.CreateNestedDataSets;
begin

  cdsTables := TClientDataSet.Create(Self);
  cdsNotes := TClientDataSet.Create(cdsTables); //nested dataset
  //Define Tables
  with TFloatField.Create(Self) do
  begin
    Name := 'TblID';
    FieldKind := fkData;
    FieldName := 'ID';
    DataSet := cdsTables;
    Required := True;
  end;

  ...  //define other fields for cdsTables  & nested clientdataset cdsNotes

  cdsNotes.AfterInsert := cdsNotesAfterInsert(cdsNotes: TDataSet);

  //Create the ClientDataSet and its nested datasets
  cdsTables.CreateDataSet;

  //This is problem code line:
  cdsNotes.AfterInsert := cdsNotesAfterInsert;


  //Configure the DataSources
  dsTables.DataSet := cdsTables;
  dsNotes.DataSet := cdsNotes;
end;


各种论坛讨论都建议了诸如以下示例的方法:

MyLabel := TLabel.Create(self);
MyLabel.OnClick := MyLabelClick;


对于AfterInsert,其中包含一个参数。如果我在设计时生成事件,Delphi会生成:

procedure TForm1.ClientDataSet1AfterInsert(DataSet: TDataSet);


试图重复上面的建议,我尝试了这种方法,该方法会生成编译器错误:

cdsNotes.AfterInsert := cdsNotesAfterInsert;不兼容类型参数列表不同

其他格式也会产生错误:

cdsNotes.AfterInsert := cdsNotesAfterInsert(DataSet: TDataSet);实际参数太多

我尝试了错误消息的其他变体。这是我第一次定义事件,我不确定我是否了解如何处理声明。我相信我声明的用于实现事件'cdsNotesAfterInsert'的实际过程不需要任何参数,因为它被束缚了
到clientdataset cdsNotes。如果我错了,请纠正我。

这是带有违规代码的完整表格单元

    unit ForumTest;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, DBClient;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    cdsTables : tclientDataset;
    cdsNotes :tclientDataset;
    procedure CreateNestedDataSets;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.CreateNestedDataSets;
begin

  cdsTables := TClientDataSet.Create(Self);
  cdsNotes := TClientDataSet.Create(cdsTables);
  //Define Tables
  with TFloatField.Create(Self) do
  begin
    Name := 'TblID';
    FieldKind := fkData;
    FieldName := 'ID';
    DataSet := cdsTables;
    Required := True;
  end;
  with TFloatField.Create(Self) do
  begin
    Name := 'TblParentID';
    FieldKind := fkData;
    FieldName := 'Parent';
    DataSet := cdsTables;
    Required := false;
  end;
  with TStringField.Create(Self) do
  begin
    Name := 'TblTitle';
    FieldKind := fkData;
    FieldName := 'Title';
    Size := 40;
    DataSet := cdsTables;
    Required := True;
  end;
  with TStringField.Create(Self) do
  begin
    Name := 'TblFilename';
    FieldKind := fkData;
    FieldName := 'Filename';
    Size := 80;
    DataSet := cdsTables;
    Required := False;
  end;
  //Note: For TDataSetFields, FieldKind is fkDataSet by default
  with TDataSetField.Create(Self) do
  begin
    Name := 'TblNotes';
    FieldName := 'NestedDataSet';
    DataSet := cdsTables;
  end;

  //Define Notes
  cdsNotes.DataSetField := TDataSetField(FindComponent('TblNotes'));
  with TFloatField.Create(Self) do
  begin
    Name := 'NoteID';
    FieldKind := fkData;
    FieldName := 'Note ID';
    DataSet := cdsNotes;
    Required := True;
  end;
  with TStringField.Create(Self) do
  begin
    Name := 'NoteTxt';
    FieldKind := fkData;
    FieldName := 'Notes';
    DataSet := cdsNotes;
    Size := 40;
  end;
  cdsNotes.AfterInsert := cdsNotesAfterInsert(cdsNotes: TDataSet);
  //Create the ClientDataSet and its nested datasets
  cdsTables.CreateDataSet;
  //Configure the DataSources
  dsTables.DataSet := cdsTables;
  dsNotes.DataSet := cdsNotes;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CreateNestedDataSets;
end;

end.

最佳答案

这是专门为您回答有关如何将一些自行编写的代码分配给TClientDataSet的AfterInsert事件属性的观点。

如果在联机帮助中查找TClientDataSet.AfterInsert,您将看到它定义为TDataSetNotifyEvent,而后者又定义为

type TDataSetNotifyEvent = procedure(DataSet: TDataSet) of object


of object的意义在于,该过程必须是对象的方法(其对象为“类”),而不是您似乎在注释中描述的独立过程/方法。

为了与TDataSetNotifyEvent分配兼容,您的过程需要具有匹配的代码“签名”,即,它必须是类的过程(而不是类的函数),并且具有完全相同的参数,在这种情况下一个TDataSet参数。

因此,将所有这些整合在一起,您所需要的只是

type
  TForm1 = class(TForm)
    ClientDataSet1: TClientDataSet;
    procedure FormCreate(Sender: TObject);
  protected
    procedure MyInsertHandler(ADataSet : TDataSet);
  end;

[...]

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataSet1.AfterInsert := MyInsertHandler;
end;

procedure TForm1.MyInsertHandler(ADataSet: TDataSet);
begin
  // Your code goes here, e.g.
  Caption := ADataSet.Name + ' after insert';
end;


就那么简单。

07-24 09:55