How to determine Tobject type for sender in Delphi?

I am creating code for dialogue with a radio group as part of a preference form. Part of our code is that when you open the preferences form, a group of radio stations is clicked that sets up a bunch of material (i.e. if the radio button is off, then a bunch of configuration contents are hidden).

I want to know when the user clicks on the radio group, and not on it, when the settings 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 also executes when the settings dialog opens. What should I enter to execute only when the user clicks the mouse button?

thanks

+7
source share
3 answers

Sender Testing

You can check 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 check the sender type.

 procedure TdlgPreferences.rgMyGroupClick(Sender:TObject) begin if sender is TRadioButton then //do action else if sender is TForm then .... 

Keyword testing is to find out if an object is of a particular type.
Note that the if AObject is TObject test if AObject is TObject always true because each object is derived from TObject.

More fun with typecasting

The fact that the tests is for the type of an object and all ancestors can be used for other purposes:

 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 .... 

Due to a short circuit of the Boolean estimate, Delphi will first check (sender is TControl) and only if it is true. The following test (TControl(Sender).Tag = 10) safe to use.

If you do not understand the construction of TControl(Sender) , you can read about type conversion.
here: http://delphi.about.com/od/delphitips2007/qt/is_as_cast.htm
and here: http://delphi.about.com/od/oopindelphi/a/delphi_oop11.htm

+16
source

If I understand you correctly, do you programmatically select your radio group to configure the initial state of the form, but do you have additional code in the click handler that you do not want to run?

If this is the case, you may want to remove the code that sets the form state to your own method, and then call it both from the radio click event and from the initialze / show / create file of your form.

Thus, you can use the same code to set the form to the required state from a user click and set it programmatically without requiring the additional logic required when the user interacts with the dialog. Or maybe I completely misunderstood your problem ...

something like that:

 // (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; 
+9
source

Try some functions or properties, for example:

  Sender.classtype InheritedFrom() 

Note: when using obj SENDER ButtonClick( sender:TObject ) (for example) Sender = button class on click => TButton

So TButton(Sender).properties or methods, etc.

+3
source

All Articles