Delphi Component owner and self parent object

Source: Internet
Author: User

Self is a reference to this object. It is the same as this in C ++. When writing any class method (except the class method), you can access it to get the current object, generally, the objects that cause the event are all passed as the sender parameter of the event. Therefore, when accessing the sender during event processing, the object that sends the event is accessed.

The component has the performance of other components. The owner of a component is specified by its owner attribute. If a component has other components, it is responsible for releasing the components it owns when it is destructed. Generally, a window has all the components on it.
If you place components in the window during the window design, the window will automatically become the owner of the component. When creating a component during running, you must pass the component ownership to the create constructor of the component. It is set as the owner attribute of the new component. The following line
The statement indicates how to transmit the parameter self hidden in the window to the tbutton. Create () constructor. In this way, the window becomes the owner of the newly created component:
Mybutton: = tbutton. Create (Self );
When the window is destructed, The tbutton instance related to mybutton is also destructed. This is handled internally in VCL. Essentially, the window traverses components and analyzes them through the components array attributes.
You can create a component that does not have the owner by passing the CREATE () method of nil to the component. However, if this is done, you need to delete the components during programming. Below Code This technology is displayed:
Mytable: = tTable. Create (nil)
Try
....
Finally
Mytable. Free;
End;
To use this technology, you should use a try... Finally block to ensure that the allocated resources are released when an exception occurs. You should not use this technology unless otherwise-you cannot specify the owner for the component.
Another property related to the link is the components property. The components attribute is an array attribute that contains a list of all components owned by a component. For example, to traverse all components in a window to display their class names, run the following code:
VaR
I: integer;
Begin
For I: = 0 to ComponentCount-1 do
Showmessage (components [I]. classname );
End;
Obviously, you can perform more meaningful operations on these components. Here is a simple description of this technology.
It can be said that:
// EDIT: = tedit. Create (NiL); // edit has no owner
// EDIT: = tedit. Create (Self); // The owner of edit is the form of edit.
// EDIT: = tedit. Create (application); // The owner of edit is application,
// EDIT: = tedit. Create (form1); // The owner of edit is form1. If edit is on form1, it is the same as passing self.

do not confuse the parent-child relationship with the ownership relationship. A component can be the parent of other components, but only window components (such as
twincontrol) can become the parent of other components. The parent component is responsible for calling the child component method to draw the child component. The parent of a component is specified through its parent attribute.
A component's parent does not have to be its owner. For components, it is legal to have different parent and owner.

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.