Analysis of Sender:tobject object in Delphi

Source: Internet
Author: User

Analysis of Sender:tobject object in Delphi

Procedure Tform1.button1click (Sender:tobject);

Begin

End

Parsing: Procedure is a procedure, Tform is a form class, plus a number is a form, like TForm1 is a Form1 form.

Button1 is the name of your button control, Button1Click is the button's Click event, (Sender:tobject) is the send message to the object, which defaults to this form.

The type of sender is TObject, which is a derived class of TObject.

Sender object in Delphi

Definition of 1.Sender:

Each event handler has at least one sender parameter. Like what:

Procedure Tform1.button1click (Sender:tobject);

Begin

...

End

Sender's meaning is to represent the control that invokes the Tform1.button1click process.

Since sender is TObject, So any object can be assigned to sender. When you click BUTTON1, a Button1Click event is generated, and the system passes the Button1 to the Button1Click process as a parameter, that is, sender.

Usage of 2.Sender:

<1> Because sender represents the control that invokes the process, you can simply take it as a control, but if you want to use a property, it's best to write it (the sender as control name). Control Properties: = ... For example:

Procedure Tform1.edit1click (Sender:tobject);

Begin

With Sender as Tedit do

Begin

text:= ' Hello ';

End

End

Another example:

Procedure Tform1.button1click (Sender:tobject);

Begin

If Sender is TButton then

ShowMessage (Sender as TButton). Caption)

Else

ShowMessage (' no sender ')

End

If you call this

Procedure TForm1.button2.click (Sender:tobject);

Begin

Button1Click (nil);

End

Then return ' no sender '

And if you call this

Procedure TForm1.button2.click (Sender:tobject);

Begin

Button1Click (sender);

End

The display is: button2

<2> If you are dealing with the same thing in two events, you can use sender to avoid rewriting the same process. For example:

Procedure Tform1.button1click (Sender:tobject);

Begin

Do same sth ...;

If Sender=button1 Then

Do sth ...;

If Sender=button2 Then

Do other sth ...;

End

Procedure Tform1.button2click (Sender:tobject);

Begin

Button1Click (Button2);

End

Two uses of 3.Sender parameters

Sender is a parameter of type TObject that tells Delphi which control receives the event and invokes the appropriate process.

You can write a single event handling handle through the sender parameter and if ... Then ... Statement or case statement to work with multiple artifacts.

The value of the component or control in which the event occurred has been assigned to the sender parameter, and the first purpose of the argument is to test the sender with the reserved word is.

To find the type of widget or control that called this event-handling handle.

For example, the handle for the click event of the edit box and label in the form points to the form's XXX procedure, and the edit box and label react differently to the Click event:

Procedure TForm1 xxx (sender:tobject);

Begin

If (sender is Tedit) then

ShowMessage (′this is a editbox′);

If (sender is Tlabel) then

ShowMessage (′this is a label′);

End

The second purpose of the sender parameter is to combine the as operator for type conversion and to cast several subclasses derived from a parent class into the parent class.

For example, there is a Tedit class control and a TMemo control in the form, which are actually derived from the Tcustomedit class.

If you want to provide the same handling for an event of both, you can point both event handles to the custom procedure yyy:

Procedure tform1.yyy (Sender:tobject);

Begin

(sender as Tcustomedit). Text:=′this is some demo text′;

End

In the process, the as operator casts both the Tedit class and the TMemo class into the Tcustomedit class, and then assigns a value to the properties of the Tcustomedit class. Note that this conversion must conform to the hierarchical relationship of classes in Delphi.

  

In a word, using the sender parameter can handle multi-class control through a single process segment, which truly embodies the reusability of Delphi object-oriented.

Analysis of Sender:tobject object in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.