Delphi's VCL technology allows many programmers to get started very quickly: the programmer's door is simply dragged and a few Pascal statements. Haha, a Delphi program that can run very well! Congratulations, you can already carry out this great cause of program development. However, after studying for a while, you may not think so. Because Delphi supports object-oriented languages, in fact, all VCL Components are designed based on object-oriented languages. So when we use these object-oriented things to design some not-object-oriented things, it seems a bit inappropriate. I feel that of course, here we will not discuss the quality of using object-oriented methods, nor do we want to cause protests from relevant people, after all, "carrots and vegetables".
In Delphi, all controls are declared under the Publish keyword, which is also the default location for using component programming. In this way, if a Form2 wants to refer to a control in Form1 (if it is Unit1), as long as Use Unit1 is OK. If Form1 wants to refer to the thing from Form2, it can also be made as shown. However, if one day I accidentally change the name of the control in Form2, wouldn’t all the codes in Form1 be rewritten? Therefore, I advocate using these controls as special elements of the Form class, and external forms can access the controls in them through the properties published by the Form class.
for example:
TForm1=class(Tform)
PRivte
Button1:Tbutton
end;
However, this design solves the high encapsulation of members. However, there will be errors in compilation saying that the Tbutton class cannot be found. The reason is that we put Button1 in Private. Delphi will not automatically register the Tbutton class for us. In fact, this problem is also solved. It's very simple
Initialization
Add RegisterClasses([Tbutton])
That's OK. Of course, if there are different classes of controls, you can be processed and placed in the RegisterClasses parameters as the parameters are an array of controls.
Of course, such an object-oriented design method is more complicated than the previous "drag" method, but we have achieved a high degree of encapsulation, which I think is worth it, what about you guys?