5
votes

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

3
Your question makes no sense. You want to access the MainForm from the LoginForm, but your project code shows that you're not creating the MainForm until after LoginForm has displayed successfully. You can't have it both ways. If you don't create MainForm before LoginForm tries to access it, you can't access things on MainForm.Ken White
yes , because i really need it to use the main form to Check then proceedSdean
You can use a DataModule to check whatever you want, and share that DataModule between your login form and main form.jachguate
The intention of visual controls (Form, Edit, etc.) is Interaction and not storing data. Once you realize this, you'll never face this question again ;o)Sir Rufo

3 Answers

12
votes

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.

4
votes

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.

0
votes

Similar to David's answer, but with slightly different behaviour, I previously answered this solution which is capable of reuse in application's lifetime.