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
LVL 7
frizeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_marwanCommented:
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;
frizeAuthor Commented:
Thank you a_marwan  but i want it to be in the DPR project
a_marwanCommented:
Please, find attached
ExpertExchange-Contribution.zip
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

frizeAuthor Commented:
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
a_marwanCommented:
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.
frizeAuthor Commented:
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
jimyXCommented:
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

frizeAuthor Commented:
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 .
Sinisa VukSoftware architectCommented:
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

jimyXCommented:
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.
Geert GOracle dbaCommented:
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 ... ?
frizeAuthor Commented:
OK , many thanks for all of you , but i find a solution by my self , that using frame inside the main form , the frame will hold the main login routine if this routine succeeded the frame will be closed otherwise it will persist and stays as the main active GUI part .

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a_marwanCommented:
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 .
Geert GOracle dbaCommented:
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
frizeAuthor Commented:
This method will be safe and cross-platform without going into issues like : the taskbar icon not shown ...etc  .
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.