Delphi is easy to confuse concept

zhaozj2021-02-08  311

Delphi is easy to confuse concept Lin Yi (06/22/2000) 1. Owner and Parent: The Parent property is a package container for the component, and the component can only display and move within this range. The example is as follows: (1) On the form of Form1, put a panel1, and pull the panel1, (2) Place a button1 in Panel1; (3) Place a Button2 on Form1. Now if you move Panel1, Button1 moves with Panel1 because the Parent of Button1 is Panel1. Now move Button2 on Panel1, move Panel1 again, button2 does not move, this is because Button2's Parent is Form1. In addition to in the form design, it should be noted that the PARENT of the component is except that when the component is dynamically created, it should also be pointed out of the component's Parent, such as continued operation in the above example: 1) Procedure TFORM1.Button2Click (Sender: Tobjet); 2 ) - Button: tbutton.cerate (self); 6) Button.parent = panel1; 7) Button.lleft = 0; 8) Button.top = 0; 9) Button. CAPTION: = 'OK'; 10) End; When pressing button2, a Button will be created on Panel1, and if you change the sixth sentence to Button.Parent: = Self; press Button2, a Button will be created on Form1. . If you delete the 6th sentence, press button2, nothing will happen, because the creation method cannot know where the component should be displayed. The Owner property is the owner of the component, which is responsible for the creation and release of the component. As in the above example, the owner of all components on the system default form is a form, and the owner of the form is Application. Incidentally, the Create method should have parameters representing the member owner. As in the above example, the component owner is a form, that is, Self. The Parent attribute and the Owner property are attributes of the running phase, which can only be set at the runtime phase. Second, SELF and SENDER Difference: In the event handler parameter table, at least one parameter sender contains at least a component that triggers the event handler. In the above example, Sender refers to Button2, with the sender parameter, which can make more The components share the same event handler, as examples: procedure tform1.buttonclick (sender: TOBJECT); begin if sender = button1 Then Label1.caption: = 'Pretty spectacular flowers Flower Flowers' else label2.caption: =' Wang Sky Cloud Roller 'end; In this case, Button1, button2 share the ButtonClick event handler. SELF means which class is the program range in which the Delphi is programmed within the form range, so Self is the form, if you write a class or a component, Self refers to the class or component. . We can see that SELF is representative of which component, the Self representative '.'. In the first example, Self represents TFORM1.

It should be noted that Self can only be used in a class method, and cannot be used in a process or function, as used as the following example is wrong: Function A1 (B: Integer): integer; begin ... button: = tbutton.create (Self ); ... end; three, ClientWidth and Width distinguishing: For a general component, Height is ClientHeight, Width is ClientWidth, and for the form, Height is the height of the title bar, ClientHeight refers to the height of the form workspace. Similarly, ClientWidth is the width of the specified form workspace. From the above statement, understand the difference between Ower and Parent, Self and Sender, ClientHeight, and Height, ClientWidth, and Width, are important to program the correct programming in Delphi.