Search code examples
delphimdichildmdiparent

Error on opening an MDI ChildForm from another MDI ChildForm's TButton via TMenuItem on MDI ParentForm


I have 2 MDIChild Forms and an MDIParent Form with a TMainMenu. In the TMainMenu, I have the following TMenuItem.OnClick procedure to open ChildForm2:

procedure TfrmMain.miOpenChildForm2Click(Sender: TObject);
begin
  TfrmChildForm2.Create(self).Show;
end;

Now, I want to access the above procedure from ChildForm1 by a TButton.OnClick procedure below:

procedure TfrmChildForm1.btnOpenChildForm2Click(Sender: TObject);
begin
  frmMain.miOpenChildForm2Click; // Error Here: E2035 Not enough actual parameters
End;

I am getting an error on the above second procedure:

E2035 Not enough actual parameters

I don't know exactly where to correct it. I tried putting '()' at the end of the procedure call, but no avail.


Solution

  • The procedure definition

    procedure TfrmMain.miOpenChildForm2Click(Sender: TObject);
    

    tells you that the procedure expects to receive a parameter Sender of type TObject. Calling it with frmMain.miOpenChildForm2Click; does not pass that parameter. The parameter is not optional.

    Sender is intended to tell you what triggered the event, for use in cases where it matters, such as when you're using one event handler for multiple controls. It allows you to differentiate where the call to the event originated.

    You can use the button or menu item that was clicked in the call as the parameter

    frmMain.miOpenChildForm2Click(btnOpenChildForm2);
    

    If it doesn't matter where the call came from, you can pass nil instead

    frmMain.miOpenChildForm2Click(nil);
    

    As a note: MDI has been deprecated for at least a decade, and Windows has not had much support for it for at least that long. Modern applications do not use MDI, and new development most likely shouldn't include it either.