Link to home
Start Free TrialLog in
Avatar of frize
frizeFlag for Algeria

asked on

delphi Login Form

Hello , i would like to have 2 forms : TLoginFrm and TMainFrm :

1st step LoginProcess : the LoginFrm will be loaded before any other form if the login routine is OK then Create/load and show the MainFrm otherwise keep the loginFrm opened till the user close it manually .

2nd Step :  if the LoginProcess is OK and the MainFrm is shown , there'll a button on the mainFrm that's Logout if clicked it will go to the 1st step .

can you please help me creating this .

thank you
Avatar of a_marwan
a_marwan

1. Create a project with the two forms
2. Add controls to both forms as desired
3. Make sure TLoginFrm is selected as the Main From in project -> options -> forms
4. In TLoginFrm, If user clicks "Ok", check your criteria (valid user and pass) and if validated:
Begin
  //.....  Validation Phase .....
  MainFrm.Show;
  Hide;
End;
Avatar of frize

ASKER

Thank you a_marwan  but i want it to be in the DPR project
Avatar of frize

ASKER

a_marwan , after the login success the main form has no icon on the taskBar ( windows 7 ) and when it's minimized it will not be minimized into the taskbar
That's because the main form was hidden. I don't know why you took this approach!

Usually, We display the main form (empty) AND the login over it at startup.
Now, if user enters correct credentials we let him in and show main form contents, if not we let him with a form which contains only a "login" button.
Avatar of frize

ASKER

a_marwan , because my project will work in such a way that nothing will be created ( except for the LoginFrm ) or active until a success login . it's a chat project .

thank you again
Avatar of jimyX
Zarko from "Delphi About" has an article, exactly what you need.

I am bringing the code over in case the link goes down someday:
DPR
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.

Open in new window


Login Unit
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
     ModalResult := mrOK
   else
     ModalResult := mrAbort;
 end;
 
 end.

Open in new window

Avatar of frize

ASKER

jimyX , But this doesn't answer my 2nd question

2nd Step :  if the LoginProcess is OK and the MainFrm is shown , there'll a button on the mainFrm that's Logout if clicked it will go to the 1st step .
you can put this code to this button:
procedure TForm.ButtonLogoutClick(Sender: TObject);
begin
  Self.Hide; //temp. hide
  if TLoginForm.Execute then
    Self.Show  //show  again - here can be form initializazion too....
  else
    Close;
end;

Open in new window

The secret is in the line:

TLoginForm.Execute

Which is the key to bring the Login Form to the user to evaluate credentials, and it can be called over and over, whenever authorization is required, the rest is just toggling of forms appearance, be it MainForm or any other form, all depends on the result whenever "LogInButton" at FormLogin is clicked.

Be careful when closing the Forms, if you close the Main Form the entire application is terminated.

Also you can make a counter if you want a certain number of tryouts, before closing.
a chat app usually opens the main form empty and then shows the login
logout closes everything

what if you get an error during loading of the main form after the login ?
where will you show those errors ... ?
ASKER CERTIFIED SOLUTION
Avatar of frize
frize
Flag of Algeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But this will not fulfill your 1st requirement ?!

1st step LoginProcess : the LoginFrm will be loaded before any other form if the login routine is OK then Create/load and show the MainFrm otherwise keep the loginFrm opened till the user close it manually .
frize ...
your solution does not fit the items in your question

several people indicated items in your question were difficult to do, as this would in essence break how the "main form" works in delphi,

you then accepted your own answer which in essence followed the advice given by those experts
without giving any credit to anyone else

some experts tend to remember not getting any credit
in the past some experts got banned because they created a blacklist for non-crediting members
i don't see any benefit in banning and neither in creating a flame war
it's just that in the past ... things like this have led to flame wars
Avatar of frize

ASKER

This method will be safe and cross-platform without going into issues like : the taskbar icon not shown ...etc  .