Board index » delphi » Accessing functions of controls that have been created at runtime

Accessing functions of controls that have been created at runtime

Hi,

I have a function that creates a message dialog with a few components
on it (timage, tlabel, tbitbtn, tcheckbox).  I'd like to be able to
execute a procedure whenever the tcheckbox is created, but I can't
figure out how to access/redirect the onclick method of the checkbox.
Can anyone help me out with this, or provide a better way of doing
this.  Thanks!

Grant Yatabe
yata...@mindspring.com

 

Re:Accessing functions of controls that have been created at runtime


Hi Grant,

Quote
> I have a function that creates a message dialog with a few components
> on it (timage, tlabel, tbitbtn, tcheckbox).  I'd like to be able to
> execute a procedure whenever the tcheckbox is created, but I can't
> figure out how to access/redirect the onclick method of the checkbox.
> Can anyone help me out with this, or provide a better way of doing
> this.  Thanks!

Simply redirect the event to a procedure of your own:

TMyDialog = Class(TForm)
protected
  procedure MyCheckBoxClick(Sender: TObject);
end;

***
// somewhere in TMyDialog

MyCheckBox := TCheckBox.Create;
MyCheckBox.OnClick := MyCheckBoxClick;

***

Understand that an event is simply a pointer to a procedure or function.
You can assign any procedure or funtion to the event as long as the
parameters match the events type for example:

TNotifyEvent = procedure (Sender: TObject) of object;

***
OnClick: TNotifyEvent;
***

The OnClick event in the above example is defined as being of type
TNotifyEvent.  Thus, whatever procedure is assigned to the OnClick event
must contain the parameters defined by TNotifyEvent.

I hope this helps,

--
Earl Reddell
Res-cue(Resourceful Components for User Ease!)
Check out our web site: http://www.res-cue.com

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---

Re:Accessing functions of controls that have been created at runtime


Im Artikel <373de1e2.59250...@192.168.0.1>, yata...@mindspring.com (Grant
Yatabe) schreibt:

Quote
>how to access/redirect the onclick method of the checkbox.

Redirect the handler like:
  MyDialog.Checkbox1.OnClick := MyOnClickMethod;
where MyOnClickMethod is a method in e.g. the form, that creates the dialog
box.

If you want to use the same handler for many checkboxes, you can also introduce
another common handler for the whole dialog:

TMyDialog = class(TForm)
public
  NewClick: TNotifyEvent;
  procedure OldClick(Sender: TObject);
...
All checkboxes should use the OldClick handler for their OnClick event. That
common handler then calls the new handler:

procedure TMyDialog.OldClick(Sender: TObject);
begin
  if assigned(NewClick) then NewClick(Sender);
end;

Then you must assign the new handler only once, to MyDialog.NewClick, and not
to every MyDialog.CheckBox#.OnClick.

It should also be possible to use a global procedure instead of a TNotifyEvent
procedure.

DoDi

Other Threads