1. The difference between Owner and Parent:
The Parent property refers to the component's container, and the component can only be displayed and moved within this range. An example is as follows:
(1) On the form of Form1, place a Panel1 and enlarge Panel1.
(2) Place a Button1 on Panel1;
(3) Place a Button2 on Form1.
Now if you move Panel1, Button1 moves with Panel1. This is because Button1's Parent is Panel1. Now move Button2 to Panel1, and move Panel1 again. Button2 does not move with it. This is because the Parent of Button2 is Form1. In addition to the form design, you should pay attention to who the parent of the component is. When dynamically creating a component, you should also indicate the parent of the component. For example, continue the operation in the above example:
1)PRocedure Tform1.Button2click(Sender:Tobjet);
2)Var
3) Button:Tbutton;
4) Begin
5) Button:Tbutton.cerate(self);
6) Button.parent=panel1;
7) Button.lleft=0;
8) Button.top=0;
9) Button.caption:='OK';
10) End;
When Button2 is pressed, a Button will be created on Panel1, and if sentence 6 is changed to Button.parent:=self; when Button2 is pressed, a Button will be created on Form1. If you delete sentence 6, nothing will happen when you press Button2. This is because the creation method cannot know where the component should be displayed.
The Owner attribute refers to the owner of the component, which is responsible for the creation and release of the component. As in the above example, the system default owner of all components on the form is the form, and the owner of the form is application. By the way, the create method should have a parameter that represents the owner of the component. In the above example, the owner of the component is the form, which is self.
The Parent attribute and Owner attribute are run-time attributes and can only be set through code during the run-time phase.
2. The difference between Self and Sender:
In the event handler parameter table, there is at least one parameter Sender, which represents the component that triggers the event handler. In the above example, Sender refers to Button2. With the Sender parameter, multiple components can share the same event handler. , as an example:
Procedure Tform1.Buttonclick(Sender:Tobject);
Begin
If sender=button1 then
Label1.caption:=′Look at the flowers blooming and falling in front of the court
′ Else Label2.caption:=‘Looking at the clouds rolling in the sky’
End;
In this example, Button1 and Button2 share the Buttonclick event handler.
Self refers to the class in which the program is programmed. In Delphi, most programming is within the form scope. Therefore, self refers to the form. If you are writing a class or a component, self refers to the class or component. . We can see which component self represents in the declaration of a function or procedure, that is, self represents the component before the '.' number. For example, in the first example, self represents Tform1. In addition, it should be noted that self can only be used in class methods, not in procedures or functions. The following usage is wrong:
Function a1(B:Integer):Integer;
Begin
…
Button:=tbutton.create(self);……
End;
3. The difference between Clientheight and Height, Clientwidth and Width:
For general components, Height is Clientheight, and Width is Clientwidth. For forms, Height is the height including the title bar, and Clientheight refers to the height of the form's client area. In the same way, Clientwidth specifies the width of the client area of the form.
As can be seen from the above statement, understanding the differences between Ower and Parent, Self and Sender, Clientheight and Height, Clientwidth and Width is important for correct programming in Delphi.