i am creating code for a dialog with a radio group as part of a preferences form. Part of our code is that when the preferences form is opened, the radio group is clicked, which configures a bunch of stuff (ie if the radio button is ‘off’ then a bunch of config stuff is hidden).
What I want is to know when the user actually clicks the radio group as opposed to it being fired when the preferences dialog opens.
So the code looks like this:
(open preferences)...
rgMyGroupClick(nil)
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
if sender <> nil then
begin
//do something useful
end;
But this code is also executed when the preferences dialog is opened. What should I put in there to only execute when the user actually clicks the mouse on the button?
Thanks
Testing your sender
You can test the sender in two ways:
or you can test the type of a sender.
The
iskeyword tests to see if an object is of a certain type.Note that the test
if AObject is TObjectis always true because every object is derived from TObject.More fun with typecasting
The fact that
istests for the object type and all ancestors can be used for other purposes as well:Because of short-circuit boolean evaluation Delphi will first check
(sender is TControl)and only continue if that is true. Making the subsequent test(TControl(Sender).Tag = 10)safe to use.If you don’t understand the construct
TControl(Sender)you can read up on typecasting.here: http://delphi.about.com/od/delphitips2007/qt/is_as_cast.htm
and here: http://delphi.about.com/od/oopindelphi/a/delphi_oop11.htm