Using the use of a name component
I have seen this in many Delphi information: Every Component must have a Name that is not empty, it is used to distinguish between other Components. I have also convinced this kind of saying in the past. But after I saw "Mastering Delphi 6", I know I am wrong. Component's Name property can be empty, especially for Menu Separator, Static Label, Bevel, Shape, and Panel, etc. Although IDE will give all the components a default name, but you can empty it in Object Inspector, this consequence is:
1. The component will become unnamed or component <0> in Object Inspector and Object TreeView;
2. The statement of this component will be completely deleted from the Form's statement;
3. If you look at the definition of the.dfm file with View as text, you found that there is no name-free Component object is defined in this form:
Object: tMenuItem
CAPTION = '-'
end
And the general statement is:
Object File1: TMenuItem
CAPTION = 'file'
end
The real benefit of this feature is that you can make the form of forms a simple declaration, and you will never fully contain a large number of useless N1, N2 identifiers, thus confusing your sight. Of course, the size of the executable will also be slightly reduced.
However, if all Label's Name on the Form is set to empty, running the program will generate an error because the compiler sees that there is no label in the declaration, so it will not connect TLabel's library function to the TLabel's library function. (The compiler is very smart in most times, but this time can only be said to be a self-satisfaction.) The solution is to keep at least a named Label.
We can even manually delete a declaration of a Component in Form. Don't worry, this Component will not disappear when Form is built, but it is not possible to use the usual way to reference it. When you really need it, you can find a specific component with Form1.FindComponent ('Button1'). If the components in the Form are too much, you can use this technique to delete a part of the less important statement, keep the unit's simpleness.
In fact, it is necessary to reduce the number of Label statements, using the new LableDit in Delphi 6 is a valid approach, and LablededIit can simplify the layout management of the form. However, LabeledEdit is only valid for Edit. To reduce other declarations that do not need to respond to events and processing inputs, such as Menu Separator, Panel, and Bevel, etc., only this method can only be used.
Unlike general procedure or function, the method of belonging to a class in Delphi is called Method, and their statements are generally
APROCMETHOD: Procedure (Sender: TOBJECT) OF Object;
Why do you have to add to Object? In fact, the general function pointer in Object Pascal is equivalent to a normal pointer, and APROCMETHOD is a pointer, which has a corresponding type in Delphi: TMethod.
TMETHOD = Record
DATA, CODE: POINTER;
END;
So, we can do this in the unit:
Procedure TFORM1.BUTTON1CLICK2 (Sender: TOBJECT); Begin
SHOWMESSAGE ('Click2!');
END;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
VAR
Method: TMETHOD;
Begin
Method.Data: = Self;
Method.code: = MethodAddress ('Button1Click2');
Button1.onclick: = TNOTIFYEVENT (METHOD);
END;
Of course, actually only need button1.onclick = button1click2; however, this can make you understand what happened behind the scenes. It is worth noting that Button1Click2 should be declared in the Publish section (or put together with other components, because the default scope is published) so that the compiler generates RTTI information for it, otherwise the program may not meet the expected effect.