Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi Login Form

in my Delphi program i've a Login Form and it's Displayed Before the Main Form is Created , but the issue that i'm facing is that i want to Login Check to be processed in the main form , that means the Login Form will use the Main Form to check and proceed ,

please read the comment placed in :

procedure LogInButtonClick(Sender: TObject) ;

here is the TLoginForm code ( from 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. 

and here's the Main Program Initialization flow :

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.

thank you

like image 225
Sdean Avatar asked Jan 21 '13 21:01

Sdean


2 Answers

If you need the main form to be created first, then create it first:

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;

That's a direct and naive answer to the question that you asked. Thinking more broadly, I would encourage you to move the login testing out of the main form. Put it somewhere that can be used by whatever higher-level code needs to. The design you are currently working towards has unhealthy coupling.

like image 147
David Heffernan Avatar answered Oct 19 '22 10:10

David Heffernan


I usually do this from the OnCreate of the MainForm; Or from the OnCreate of the DataModule, if you have one. For example:

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

I don't like messing with the DPR file too much. This works, shows the forms in the correct order, and if the TMainForm was auto-created by Delphi then the MainForm variable is already assigned and ready to use when the OnCreate fires;

PS: Accesing the MainForm variable is actually bad design, but it's there if you want it.

like image 32
Cosmin Prund Avatar answered Oct 19 '22 09:10

Cosmin Prund