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:
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
if sender = RadioButton1 then //do action
else if sender = RadioButton2 then ....
or you can test the type of a sender.
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
if sender is TRadioButton then //do action
else if sender is TForm then ....
The is
keyword tests to see if an object is of a certain type.
Note that the test if AObject is TObject
is always true because every object is derived from TObject.
More fun with typecasting
The fact that is
tests for the object type and all ancestors can be used for other purposes as well:
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
//TObject does not have a 'tag' property, but all TControls do...
if (sender is TControl) and (TControl(Sender).Tag = 10) then ....
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
If I'm understanding you correctly you are clicking your radiogroup programatically to set up the initial state of the form, but you have extra code in the click handler that you don't want to run?
If that's the case you might want to consider removing the code that sets the state of the form to its own method and then call it from both the radio click event and from the initialze/show/create of your form.
That way you can use the same code to set the form to your required state from a user click and programatically set it without doing any extra logic required when a user is interacting with the dialog. Or maybe I've completely misunderstood your problem...
something like this:
// (open preferences)...
SetStateOfFormForSelectedGroup();
procedure TdlgPreferences.SetStateOfFormForSelectedGroup()
begin
//do all setting of form for selected group here.
end;
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
SetStateOfFormForSelectedGroup();
//do something useful
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With