德尔福登录表单(delphi Login Form)

2019-07-18 12:02发布

在我的Delphi程序,我登录表单,它的显示在主窗体之前被创建,但我现在面临的问题是,我想登录检查的主要形式进行处理,这意味着登录表单将使用主窗体检查和进行,

请阅读放在评论:

程序LogInButtonClick(发信人:TObject的);

这里是TLoginForm码( 从delphi.about.com ):

    unit login;

 interface

 uses
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, StdCtrls;

 type
   TLoginForm = class(TForm)
     LogInButton: TButton;
     pwdLabel: TLabel;
     passwordEdit: TEdit;
     procedure LogInButtonClick(Sender: TObject) ;
   public
     class function Execute : boolean;
   end;

 implementation
 {$R *.dfm}

 class function TLoginForm.Execute: boolean;
 begin
   with TLoginForm.Create(nil) do
   try
     Result := ShowModal = mrOk;
   finally
     Free;
   end;
 end;

 procedure TLoginForm.LogInButtonClick(Sender: TObject) ;
 begin
   if passwordEdit.Text = 'delphi' then
   {
   Here how it's possible to use :
    if MainForm.text=passwordEdit.Text then 
    ModalResult := mrOK
    }

     ModalResult := mrOK
   else
     ModalResult := mrAbort;
 end;

 end. 

而这里的主要程序初始化流程:

program PasswordApp;

 uses
   Forms,
   main in 'main.pas' {MainForm},
   login in 'login.pas' {LoginForm};

 {$R *.res}

 begin
   if TLoginForm.Execute then
   begin
     Application.Initialize;
     Application.CreateForm(TMainForm, MainForm) ;
     Application.Run;
   end
   else
   begin
     Application.MessageBox('You are not authorized to use the application. The password is "delphi".', 'Password Protected Delphi application') ;
   end;
 end.

谢谢

Answer 1:

如果你需要的主要形式先创建,则先创建它:

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);//created, but not shown
  if TLoginForm.Execute then//now the login form can refer to the main form
    Application.Run//this shows the main form
  else
    Application.MessageBox('....');
end;

这是一个直接的和幼稚的问题的答案你问的问题。 更广泛的思考,我会鼓励你移动登录测试出来的主要形式。 把它的地方,可以通过任何更高级别的代码需要使用。 您正在赶制设计有不健康的耦合。



Answer 2:

我通常做这从OnCreate的的MainForm ; 或从OnCreate的的DataModule ,如果你有一个。 例如:

TMainForm.OnCreate(Sender: TObject);
var F: TLoginForm;
begin
  F := TLoginForm.Create(Self);
  try
    F.ShowModal;
  finally F.Free;
  end;
end;

我不喜欢用搞乱DPR文件太多。 这工作,显示了正确的顺序形式,而如果TMainForm是由德尔福自动创建的,则MainForm变量已分配和准备当使用OnCreate火灾;

PS:Accesing的MainForm变量实际上是不好的设计,但如果你想它的存在。



Answer 3:

类似大卫的答案 ,但略有不同的行为,我以前回答了这个解决方案 ,它能够在应用程序的生命周期重用。



文章来源: delphi Login Form