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:
ComponentsToCode
function from GExperts can create code from a given component as gabr pointed in this answer.Warning: This answer is for completeness sake to the question and is only for experimental purposes. It should never be used in real world scenarios.
You want two separate form definition files for only one source code file.
The key is to make use of the CreateNew
constructor. To quote the documentation on it:
Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it.
First, write your advanced form:
unit Advanced;
interface
uses
Classes, Controls, Forms, StdCtrls;
type
TAdvancedForm = class(TForm)
StandardGroupBox: TGroupBox;
StandardButton: TButton;
AdvancedGroupBox: TGroupBox;
AdvancedButton: TButton;
procedure StandardButtonClick(Sender: TObject);
procedure AdvancedButtonClick(Sender: TObject);
end;
implementation
{$R *.dfm}
procedure TAdvancedForm.StandardButtonClick(Sender: TObject);
begin
Caption := Caption + ' Button1Click';
end;
procedure TAdvancedForm.AdvancedButtonClick(Sender: TObject);
begin
Caption := Caption + ' Button2Click';
end;
end.
Build your app, and copy Advanced.dfm
to Standard.dfm
.
Open Standard.dfm
in 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
:
object StandardForm: TStandardForm
...
object StandardGroupBox: TGroupBox
...
object StandardButton: TButton
...
end
end
end
Add the resource for the standard form to Advanced.pas
:
{$R *.dfm}
{$R Standard.dfm}
And now with the following code, you can open both form definitions for the same source file:
uses
Advanced;
procedure TForm1.OpenAdvancedFormClick(Sender: TObject);
var
Form: TAdvancedForm;
begin
Form := TAdvancedForm.Create(Application);
Form.Show;
end;
procedure TForm1.OpenStandardFormClick(Sender: TObject);
var
{
Form: TAdvancedForm; // This is tricky! The form we are about to create has
// no AdvancedGroupBox nor AdvancedButton, so make sure
// you are not calling it with code completion.
Form: TStandardForm; // Compiler has no knowledge of TStandardForm!
}
Form: TForm; // So declare your form as TForm!
begin
// But create it as TAdvancedForm, otherwise components will not be found!
Form := TAdvancedForm.CreateNew(Application);
ReadComponentRes('TStandardForm', Form);
Form.Show;
end;