Creating a Login form with Delphi

I am having trouble creating a Login screen for my application. The problem is that I want this form to load first (like any login screen :) Then, if the entered password is not correct,  the program should close. Otherwise, the login window should close itself (not the program) and pass control to the main window, which has not been made visible or loaded yet.

The difficulty is that If I make the LOGIN to be created first and then HIDE it later and 'modalshow' the real main form of the application... then, when the main window is minimized, its icon remains on the desktop.

Any feedback?
rhoworksAsked:
Who is Participating?
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.

rhoworksAuthor Commented:
Edited text of question.
0
TheNeilCommented:
Why not create and display your login form in the Create method for your main form, and get it to return a flag indicating success or not? If the flag is set the th Create can continue, otherwise you can trap it and kill your application.

e.g.

procedure OnCreate;
....
  Login_Form.ShowModal;
  IF NOT(bProceed)
  THEN
  BEGIN
    Application.Terminate;
    EXIT;
  END;
....
END;

In your login form you need to create a global boolean variable (in this case bProceed), clear it in the login form's create method and set it if you want to carry on
0

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
synatureCommented:
TheNeil's solution is clean and easy, and how I do it now.  You could also create, display and handle your login form in the .dpr file, in the lines before the other forms are created and before the application.run.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

TheNeilCommented:
Do I get the points now or later? (Joke)

Total agreement with Synature - it'll work in the .dpr file but it's just personal choice that I suggested putting it into the OnCreate method.

The Neil
0
bozo7Commented:
The only problem that I see with putting it in the oncreate of your Main form is that the datamodule with the table of users would not have been created yet. Hence putting it in the .dpr file would be better because you could have the user login screen come up right after the datamodule is created.
0
TheNeilCommented:
DataModule? What DataModule?

It all depends WHERE you're getting your user data from. If you're pulling them from a file or similar then OnCreate will work fine. It really is just a matter of personal preference and experience though - In the situations where I've needed to use it, OnCreate works fine (I'm not saying that it's ALWAYS the better place but it has been for me).

Regardless of WHERE the code goes, the underlying idea is still valid.

The Neil
0
rhoworksAuthor Commented:
It works nicely. Thank you.
0
synatureCommented:
All my main forms are supported by datamodules, and I simply create the datamodule in the line before I do the login component.  The only form I ever leave in the .dpr for autocreation is the main form.
0
rhoworksAuthor Commented:
I agree with synature. My final solution was inspired by The Neil, but not placed at Main.OnCreate because it always showed (at least for an instant) the Main form itseft, even when the "Application.Terminate and Exit" part was reached. This is the code I'm using:

begin
  Application.Title:='Options Designer Pro';
  Application.Initialize;
  Login:=TLogin.Create(Application);
  Login.ShowModal;
  If Not Login.LoginOK then
  Begin
    Application.Terminate;
    Exit;
  End;

  Application.CreateForm(TMain, Main);
  Application.Run;
end.
0
TheNeilCommented:
Agreed. The form could briefly appear and it all depends on making sure that you do the login before ANYTHING else. I suppose if I'd spotted the brief popup then it I'd have cheated and positioned the main form off screen but in the systems where I've used it it hasn't been noticeable. Comments and suggestions now taken on board though.

The Neil
0
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.