We have an application that have many components on the forms (panels, tabs, edits, comboboxes, etc…). But depending on the user profile, most of them could be filled automatically and/or not be visible. So, users could do their work faster.
The question: Is there any easier way to create, position, change ownership etc, at runtime? I would like to create 2 .dfm files for a given unit and then have something to tell the application what .dfm to use. Like: “Hey! User is advanced, use the Unit1Advanced.dfm!” A working example would be nice. I would like to use that in Delphi 7 too, but it has to work at least in Delphi XE.
What I know that exist till now:
ComponentsToCodefunction from GExperts can create code from a given component as gabr pointed in this answer.- I could create 2 forms and create the desired one at runtime. But that means one additional .pas file to each additional .dfm file. This would be harder to maintain.
- This answer seems to give a hint. But I am not used to TReader and TWriter classes…
You want two separate form definition files for only one source code file.
The key is to make use of the
CreateNewconstructor. To quote the documentation on it:First, write your advanced form:
Build your app, and copy
Advanced.dfmtoStandard.dfm.Open
Standard.dfmin a text editor and remove the advanced components (in this case the advanced group box containing a button), and rename the form and form type to(T)StandardForm:Add the resource for the standard form to
Advanced.pas:And now with the following code, you can open both form definitions for the same source file: