How to identify the Tobject type for sender in Delphi? -


i creating code dialog radio group part of preferences form. part of our code when preferences form opened, radio group clicked, configures bunch of stuff (ie if radio button 'off' bunch of config stuff hidden).

what want know when user clicks radio group opposed being fired when preferences dialog opens.

so code looks this:

(open preferences)... rgmygroupclick(nil)  procedure  tdlgpreferences.rgmygroupclick(sender:tobject)  if sender <> nil begin  //do useful end; 

but code executed when preferences dialog opened. should put in there execute when user clicks mouse on button?

thanks

testing sender

you can test sender in 2 ways:

procedure tdlgpreferences.rgmygroupclick(sender:tobject) begin   if sender = radiobutton1 //do action   else if sender = radiobutton2 .... 

or can test type of sender.

procedure tdlgpreferences.rgmygroupclick(sender:tobject) begin   if sender tradiobutton //do action   else if sender tform .... 

the is keyword tests see if object of type.
note test if aobject tobject true because every object derived tobject.

more fun typecasting

the fact is tests object type , ancestors can used other purposes well:

procedure tdlgpreferences.rgmygroupclick(sender:tobject) begin   //tobject not have 'tag' property, tcontrols do...   if (sender tcontrol) , (tcontrol(sender).tag = 10) .... 

because of short-circuit boolean evaluation delphi first check (sender tcontrol) , only continue if true. making subsequent test (tcontrol(sender).tag = 10) safe use.

if don't understand construct tcontrol(sender) can read on typecasting.
here: http://delphi.about.com/od/delphitips2007/qt/is_as_cast.htm
, here: http://delphi.about.com/od/oopindelphi/a/delphi_oop11.htm


Comments