-1

I would like to know if there's any way to link a component's event (As TButton.OnClick) to a procedure defined in a datamodule (At design-time). At the moment, I'm doing this at runtime:

MyButton.OnClick := MyDataModule.MyOnClickEvent;

The goal would be that to get MyDataModule.MyOnClickEvent proposed in the dropdown lists in the Object Inspector Window. Thanks in advance to all who will share their knowledge :D

Note1: Here's a similar question, but there are no informations about how to accomplishing the same task at design-time: Access an event on a DataModule from another Form

Note2: The only way I found consists in define all events on a parent-form, but I was hoping there was a cleaner solution

Hwau
  • 850
  • 3
  • 12
  • 23

2 Answers2

1

As far as I know something like this is not possible.

Perhaps with some addons but I doubt it. Why?

Imagine all the cluter that would be returned when you try using something like this on a project with hudreds of forms and thousands of events.

Also how would you deal when you would have multiple events on different forms with same name?

SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • 1
    I agree, a property of a datamodule with the word 'click' in it doesn't make a lot of sense, since there's nothing to click on at runtime in a datamodule... – John Easley Apr 04 '15 at 23:19
  • Only events that belong to datamodules which units are in the uses clause would be returned. Exactly as it happens for components, after adding a datamodule's unit to the uses clause, you can link form's components to datamodule's components -> In the dropdown list you can see "MyDataModule.MyComponent". – Hwau Apr 05 '15 at 21:41
0

Define a published method in the DataModule to act as the event handler. Its signature must match TNotifyEvent.

Make sure the DataModule's unit is in the interface uses clause of the component's parent Form unit.

Make sure the DataModule is created before the parent Form is created.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    In datamodule class I've added: published procedure OnClickTest(Sender : TObject);. In form unit I've added uses uMyDataModuleUnit. "OnClickTest" event is not in the dropdown list, did I've done something wrong? Thanks. – Hwau Apr 05 '15 at 21:32