1
votes
procedure TFormOrderAdd.DBEdit1DblClick(Sender: TObject);
var
  FormSelectEmp: TForm;
  SelectEmpDBGrid: TDBGrid;

begin
  FormSelectEmp := TForm.Create(Self);
  SelectEmpDBGrid :=  TDBGrid.Create(Self);
  SelectEmpDBGrid.Parent := FormSelectEmp;
  SelectEmpDBGrid.Align := alClient;
  SelectEmpDBGrid.DataSource := DMl.DataSourceViewEmpList;
  FormSelectEmp.ShowModal;
  SelectEmpDBGrid.OnDblClick := AddSelectedEmp;
  FormSelectEmp.Close;
end;

procedure TFormOrderAdd.AddSelectedEmp;
begin
  DBEdit1.Text := Dml.ADOQueryViewEmpList.FieldByName('ID').Text;
end;

How i can add my procedure to OnDblClick Event? I tryed just assign, but compiller says: [dcc32 Error] OrderAdd.pas(66): E2010 Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'

1
The same way you assign any other property, just pass the handler's name. Also, shouldn't that be before ShowModal? What does your procedure look like? You can't pass just any. - Jerry Dodge
I mean, what does AddSelectedEmp look like? Actually, it looks like you're attempting to call it rather than assign it. Remove (Self) from it. Please Edit your question with these full details. - Jerry Dodge
It looks like you have not provided an appropriate event handler procedure. It must have a proper signature. AddSelectedEmp should have (Sender: TObject) on it. Please read here: stackoverflow.com/a/42956399/988445 - Jerry Dodge
@димасоколов: That may be, but you still have to assign a procedure with the proper signature. Your TFormOrder.AddSelectedEmp should be procedure TFormOrder.AddSelectedEmp(Sender: TObject);. - Rudy Velthuis
@Victoria: Sure, but I merely reacted to his comment with another comment. Your example is nice, but I tried to spell it out using his code (using his procedure name), not yours. I have the impression he didn't quite get this. - Rudy Velthuis

1 Answers

3
votes

You must create a matching event method prototype in a class that you then assign to the event handler (for the TDBGrid control's OnDblClick event it is the TNotifyEvent), so you can write for example:

type
  TForm1 = class(TForm)
    DBEdit1: TDBEdit;
    DBGrid1: TDBGrid;
  private
    procedure Form1Create(Sender: TObject);
    procedure MyGridDblClick(Sender: TObject);
  end;

implementation

procedure TForm1.Form1Create(Sender: TObject);
begin
  { it doesn't matter if you create the component at runtime,
    this is a common principle of assigning event methods at
    runtime - they just have to match the method prototypes }
  DBGrid1.OnDblClick := MyGridDblClick;
end;

procedure TForm1.MyGridDblClick(Sender: TObject);
begin
  { to access the grid instance in case more than one grid
    uses this handler you can use TDBGrid(Sender) or safer
    cast (Sender as TDBGrid) }
  DBEdit1.Text := TDBGrid(Sender).DataSource.Dataset.FieldByName('ID').Text;
end;