[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5924
  • Last Modified:

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?
0
rhoworks
Asked:
rhoworks
  • 4
  • 3
  • 2
  • +1
1 Solution
 
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
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now