Example of solution in the attachment.
Code of Unit1:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{TForm1}
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
uses
Unit2;
{$R *.lfm}
{TForm1}
procedure TForm1.FormCreate(Sender: TObject);
begin
Label1.Caption := 'Form 1 created!';
Button1.OnClick := @Form2.Button1Click;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Application.CreateForm(TForm2, Form2);
Form2.Show;
end;
end.
Code of Unit2:
unit Unit2;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{TForm2}
TForm2 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
public
procedure Button1Click(Sender: TObject);
end;
var
Form2: TForm2;
implementation
{$R *.lfm}
{TForm2}
procedure TForm2.FormCreate(Sender: TObject);
begin
Label1.Caption := 'Form 2 created!';
end;
procedure TForm2.Button1Click(Sender: TObject);
var
LName: String;
begin
{click event handled by button on the TForm1}
LName := '';
if Sender is TButton
then LName := (Sender as TButton).Name;
ShowMessage('Clicked: ' + LName);
end;
end.
However, I do not recommend this approach. With more event handlers it will get messy. A better approach is (your choice):
- calling functions defined in a separate library module (own library) from event handling procedures, or
- "connecting" library procedures to events.
EDIT:
Handoko posted his answer right before me
Yes, his answer is the best way to approach this issue.
What I provided is only to show that it can be done. But that doesn't mean you should do it!