Select MainForm from the list of available forms.
Can I select a form (as the main form) from a list of "available" forms after connecting to the database? I have a datamodule with 3 "available" forms. There is no main form yet. First, a template is created. Now, I would like to select a form depending on the database the user is logged into and make it the main form. Can this be done and how?
source to share
The main form is considered the first form created by the call Application.CreateForm
. So add your selection logic to your .dpr file code and then call Application.CreateForm
to create whatever form the user selects.
// .dpr code
begin
Application.Initialize;
CreateMainForm;
Application.Run;
end.
This is CreateMainForm
provided by you and implements the user form selection. It might look like this:
procedure CreateMainForm;
var
Form: TForm;
FormClass: TFormClass;
begin
FormClass := ChooseMainFormClass;
Application.CreateForm(FormClass, Form);
end;
Again, ChooseMainFormClass
provided by you.
source to share
You can easily do something like this in DPR.
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {DM1: TDataModule},
Unit3 in 'Unit3.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TDM1, DM1);
case DM1.ChooseForm of
1: Application.CreateForm(TForm1, Form1);
else Application.CreateForm(TForm2, Form2);
end;
Application.Run;
end.
In this example, you first create a datamodule. When it is created, you can use the logic in the datamodule. In datamodule I made a public function that returns an integer to determine which form to load. (In practice, I would not rely on magic numbers)
source to share