Choose MainForm from a list of available forms

前端 未结 2 689
一生所求
一生所求 2021-01-21 05:04

Is it possible to choose a form (as a mainform) from a list of \"available\" forms after connecting to the database ? I have a datamodule with 3 \'available\' forms.No mainform

相关标签:
2条回答
  • 2021-01-21 05:36

    You can easily do something like that in the 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 the datamodule. When it's created you can use the logic in the datamodule. In the 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)

    0 讨论(0)
  • 2021-01-21 05:39

    The main form is deemed to be the first form created by a call to Application.CreateForm. So add your selection logic to the .dpr file code, and then call Application.CreateForm to create whichever form the user selects.

    // .dpr code
    begin
      Application.Initialize;
      CreateMainForm;
      Application.Run;
    end.
    

    Here, CreateMainForm is provided by you and implements the user form selection. It might go like this:

    procedure CreateMainForm;
    var
      Form: TForm;
      FormClass: TFormClass;
    begin
      FormClass := ChooseMainFormClass;
      Application.CreateForm(FormClass, Form);
    end;
    

    Again, ChooseMainFormClass is provided by you.

    0 讨论(0)
提交回复
热议问题