Delphi uses Sender parameters for code reuse

Source: Internet
Author: User
Tags case statement

One of the features of object-oriented programming tools is to improve code reusability. As a new-generation visual development tool, the code reusability in Delphi is quite high. We know that in Delphi, most program code corresponds to an event directly or indirectly. This program is called an event processing handle, which is actually a process. From Application Engineering to forms, components, and programs, Delphi emphasizes the reusability of each layer in the development process, you can write common event processing handles for some components to achieve program reuse. You can point the processing handle of event A to the processing handle of Event B on the Events page of the Property Window, so that event A and Event B share A process segment, this achieves the purpose of reuse. If the shared program segment has nothing to do with the control where the event occurs, such as ShowMessage ('hello, world '), the sharing is the simplest. However, in general, the sharing between code segments is related to the control in which the event occurs. You need to handle the event based on the control type. The Sender parameter is required.
The beginning of each process segment is similar to procedure TForm1FormClick (Sender: TObject). The Sender is a TObject type parameter, which tells Delphi which control receives the event and calls the corresponding processing process. You can write a single event processing handle through the Sender parameter and IF... THEN... Statement or CASE statement to process multiple components. The value of the component or control that triggered the event has been assigned to the Sender parameter. One of the purposes of this parameter IS that you can use the reserved word IS to test the Sender, to find the type of the component or control that calls the event processing handle. For example, you can point the processing handle of the edit box and the Click Event of the tag in the form to the xxx process of the form. The edit box and tag have different reactions to the Click event:
Procedure TForm1xxx (Sender: TObject );
Begin
If (sender if 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 convert the type with the AS operator and forcibly convert several child classes derived from a parent class to the parent class. For example, the form contains a TEdit control and a TMemo control, which are actually derived from the TcustomEdit class. If you want to provide the same processing for an event of the two, you can point both event handles to the custom process yyy:
Procedure TForm1.yyy (Sender: TObject );
Begin
(Sender as TcustomEdit). text: = 'This is some demo text ′;
End;
In the process, the AS operator forcibly converts TEdit class and TMemo class to TcustomEdit class, and then assigns a value to the properties of TcustomEdit class. Note that this conversion must conform to the hierarchy of classes in Delphi.
Using the Sender parameter can process multiple types of controls through a single process segment, which truly reflects the reusability of Delphi object-oriented.

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.