1
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'

10
  • 2
    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. Commented Jun 4, 2017 at 18:34
  • 2
    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. Commented Jun 4, 2017 at 18:35
  • 2
    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 Commented Jun 4, 2017 at 18:43
  • 3
    @димасоколов: 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);. Commented Jun 4, 2017 at 22:40
  • 1
    @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. Commented Jun 4, 2017 at 23:30

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. And now i have new problem: if i use ShowModal instead Show application stuck in vcl forms module :Application.HandleMessage; if Application.Terminated then ModalResult := mrCancel else if ModalResult <> 0 then CloseModal; and event not fired
As was mentioned in comment to your question, you must assign the event handler before you call ShowModal. And read the another answer here to address another problem that you meet (it's correct but a bit out of scope of what you asked).
How i can close form after AddSelectedEmp was executed? From AddSelectedEmp i cant access to form.
It's out of scope of this question, but well, if the assigned method is a part of the same form class (as shown in the example), you can simply use Close; method after you finish your work in that event handler (or set ModalResult property to whatever modal result you need to return). If you are assigning a handler method from another class, you can use commonly this to reach the form on which the grid is placed (GetParentForm(TControl(Sener))).Close; or set ModalResult respectively after you do your work in the event method.
not work, but i find Screen.ActiveForm.Close; it work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.