How can I declare an event handler with arguments for the tclientdataset component at run time

I am trying to define a ClientDataSet component on a form at runtime. I can successfully define all the fields and manage the ClientDataSet in the VCL forms program, however, when I try to add an event handler for events like AfterInsert, the compiler code in my format.

The Dataset client is created in this procedure:

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;

Various forum discussions have suggested approaches such as this example:

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

In the case of AfterInsert, the argument is included. If I emit an event during development, Delphi generates:

procedure TForm1.ClientDataSet1AfterInsert(DataSet: TDataSet);

Trying to duplicate the sentence above, I tried this approach, which generates a compiler error:

cdsNotes.AfterInsert := cdsNotesAfterInsert; The list of incompatible type parameters is different

Other formats also generate errors:

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.
+4
1

, , 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;

.

+10

Source: https://habr.com/ru/post/1694984/


All Articles