Yes it should work if you assign the event handler through code.
If your event handler does not use anything from the ClientData instance (recommended), you don't even need to create an instance.
A nil variable of type TClientData is enough.
In the sample app below, the ClientData module is not auto-created by the dpr and remains nil.
That does not prevent the event handler to work correctly.
the dpr
program Project2;
uses
Forms,
Unit10 in 'Unit10.pas' {Form10},
Unit11 in 'Unit11.pas' {ClientData: TDataModule};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm10, Form10);
Application.Run;
end.
the Form dfm
object Form10: TForm10
Left = 0
Top = 0
Caption = 'Form10'
ClientHeight = 282
ClientWidth = 418
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 168
Top = 168
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end
the Form pas
unit Unit10;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Unit11, StdCtrls;
type
TForm10 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form10: TForm10;
implementation
{$R *.dfm}
procedure TForm10.Button1Click(Sender: TObject);
begin
if ClientData = nil then
ShowMessage('ClientData is nil')
else
ShowMessage('ClientData is NOT nil');
end;
procedure TForm10.FormCreate(Sender: TObject);
begin
OnClick := ClientData.WhateverEvent;
end;
end.
the DataModule dfm
object ClientData: TClientData
OldCreateOrder = False
Height = 150
Width = 215
end
the DataModule pas
unit Unit11;
interface
uses
SysUtils, Classes, Windows;
type
TClientData = class(TDataModule)
procedure WhateverEvent(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ClientData: TClientData;
implementation
{$R *.dfm}
procedure TClientData.WhateverEvent(Sender: TObject);
begin
MessageBox(0, PChar('Sender is ' + Sender.ClassName), 'Test', MB_ICONINFORMATION or MB_OK);
end;
end.