在连接到数据库后,是否可以从"可用"表单列表中选择一个表单(作为主表单)?我有一个3'可用'形式的数据模块.暂时没有主要形式.首先创建数据模块.现在,我想根据用户登录的数据库选择表单,并使其成为mainform.可以这样做,怎么做?
您可以轻松地在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.
Run Code Online (Sandbox Code Playgroud)
在此示例中,您首先要创建数据模块.创建它时,您可以使用数据模块中的逻辑.在数据模块中,我创建了一个公共函数,它返回一个整数来确定要加载的表单.(在实践中我不会依赖魔法数字)
主表单被认为是通过调用创建的第一个表单Application.CreateForm
.因此,将您的选择逻辑添加到.dpr文件代码,然后调用Application.CreateForm
以创建用户选择的任何形式.
// .dpr code
begin
Application.Initialize;
CreateMainForm;
Application.Run;
end.
Run Code Online (Sandbox Code Playgroud)
在这里,CreateMainForm
由您提供并实现用户表单选择.它可能会像这样:
procedure CreateMainForm;
var
Form: TForm;
FormClass: TFormClass;
begin
FormClass := ChooseMainFormClass;
Application.CreateForm(FormClass, Form);
end;
Run Code Online (Sandbox Code Playgroud)
再次,ChooseMainFormClass
由您提供.