Board index » delphi » common event handler for different component types?

common event handler for different component types?

Quote
Dave O'Brien <dobr...@watcom.on.ca> wrote:
>In a form, I have some TEdit fields, some checkboxes, etc.
>I'd like to have a common OnChange/OnClick handler that sets the
>component's font color to red when it is changed. (Instead of
>having to code separate handlers for each type of component.)
>Here's the code snippet I had in mind:
>   procedure ComponentChange(Sender : TObject);
>   begin
>      Sender.font.color := clRed;
>   end;
>Problem is, to reference font.color, I need to use the object's actual
>classtype (e.g TEdit, TCheckbox). Fine, I'll use the ClassType
>method to find out the component's class at run-time. But the
>following...
>   procedure ComponentChange(Sender : TObject);
>   begin
>      (Sender as Sender.ClassType).font.color := clRed;
>   end;
>..won't work because Sender is passed as TObject. Is there some way of  
>finding out Sender's *actual* classtype (e.g. TEdit), or does passing
>it as TObject (which seems mandatory) lose that information?

Right result, wrong reasoning.  (Sender as Sender.ClassType) doesn't
work because the compiler still needs to be able to dereference
"font.color".  ".ClassType" doesn't give it the information needed to
do that.  ClassType will return the correct type, for the benefit of,
say, creating a second object or using another class function.

There's only two ways to go about doing what you want to do.  One way
(the hard way) is to dissect the TypInfo unit.  The .INT file is in
DOCS, or you can include the unit in a simple project and then use the
browser.  Used properly, this will let you access properties by a name
string, letting you do a sort of FindComponent thing on properties.

The second way, and the documented way, is to do something like...

procedure ComponentChange(Sender: TObject);
begin
  if Sender is TEdit then
    begin
      TEdit(Sender).font.color := clRed;
    end
  else if Sender is TCheckBox then
    begin
      TCheckBox(Sender).font.color := clRed;
    end
  else if...
    (etc.)
  end;
end;

Geo
--
George Mealer
gmea...@best.com

 

Re:common event handler for different component types?


In a form, I have some TEdit fields, some checkboxes, etc.

I'd like to have a common OnChange/OnClick handler that sets the
component's font color to red when it is changed. (Instead of
having to code separate handlers for each type of component.)

Here's the code snippet I had in mind:

   procedure ComponentChange(Sender : TObject);
   begin
      Sender.font.color := clRed;
   end;

Problem is, to reference font.color, I need to use the object's actual
classtype (e.g TEdit, TCheckbox). Fine, I'll use the ClassType
method to find out the component's class at run-time. But the
following...

   procedure ComponentChange(Sender : TObject);
   begin
      (Sender as Sender.ClassType).font.color := clRed;
   end;

..won't work because Sender is passed as TObject. Is there some way of  
finding out Sender's *actual* classtype (e.g. TEdit), or does passing
it as TObject (which seems mandatory) lose that information?

Thanks.

--

| Dave O'Brien - Watcom International - dobr...@watcom.on.ca
+-----------------------------------------------------------
|    Delphi, recumbent bikes, poker, hockey, Newton, wine

Other Threads