Delphi login form

in my Delphi program I have a login form and it is displayed before the main form is created, but the problem I am having is that I want the login validation to be handled in the main form, this means the login to the system The form will use the main form to check and proceed,

read the comment posted 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 possible to use :
    if MainForm.text=passwordEdit.Text then 
    ModalResult := mrOK
    }

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

 end. 

      

and here's the initialization thread of the main program:

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

+3


source to share


3 answers


If you want the first form to create first, 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;

      




This is a straightforward and naive answer to the question you asked. Thinking more broadly, I would advise you to move the login validation to the main form. Place it somewhere that any higher-level code can use. The design you're currently working on has an unhealthy grip.

+9


source


I usually do this from OnCreate

from MainForm

; Or from OnCreate

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 working with the file too much DPR

. This works, shows the forms in the correct order, and if TMainForm

Delphi was auto-generated then the variable is MainForm

already assigned and ready to use when it fires OnCreate

;

PS: Accepting a variable MainForm

is actually bad design, but it's there if you want it.

+4


source


Like David's answer , but with slightly different behavior, I previously answered this solution which is reusable over the life of the application.

0


source







All Articles