Delphi Login Form Code

How to Password Protect Your Delphi Application

Login/Password Dialog BEFORE Delphi Main Form
Login/Password Dialog BEFORE Delphi Main Form.

The MainForm of a Delphi application is a form (window) that is the first one created in the main body of the application. If you need to implement some kind of authorization for your Delphi application, you might want to display a login/password dialog before the main form is created and displayed to the user.

In short, the idea is to create, display, and destroy the "login" dialog before creating the main form.

The Delphi MainForm

When a new Delphi project is created, "Form1" automatically becomes the value of the MainForm property (of the global Application object). To assign a different form to the MainForm property, use the Forms page of the Project > Options dialog box at design time.

When the main form closes, the application terminates.

Login/Password Dialog

Let's start by creating the main form of the application. Create a new Delphi project containing one form. This form is, by design, the main form.

If you change the name of the form to "TMainForm" and save the unit as "main.pas," the project's source code looks like this (the project was saved as "PasswordApp"):

 program PasswordApp;
 
 uses
 Forms,
 main in 'main.pas' {MainForm};
 
 {$R *.res}
 
 begin
 Application.Initialize;
 Application.CreateForm(TMainForm, MainForm) ;
 Application.Run;
 end.
 

Now, add a second form to the project. By design, the second form that's added, gets listed in the "Auto-Create Forms" list on the Project Options dialog.

Name the second form "TLoginForm" and remove it from the "Auto-Create Forms" list. Save the unit as "login.pas".

Add a Label, Edit, and Button on the form, followed by a class method to create, show, and close the login/password dialog. The method "Execute" returns true if the user has entered the correct text in the password box.

Here's the full source code:

 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. 

The Execute method dynamically creates an instance of the TLoginForm and displays it using the ShowModal method. ShowModal does not return until the form closes. When the form closes, it returns the value of the ModalResult property.

The "LogInButton" OnClick event handler assigns "mrOk" to the ModalResult property if the user has entered the correct password (which is "delphi" in the above example). If the user has provided a wrong password, ModalResult is set to "mrAbort" (it can be anything except "mrNone").

Setting a value to the ModalResult property closes the form. Execute returns true if ModalResult equals "mrOk" (if the user has entered the correct password).

Don't Create MainForm Before Login

You now only need to make sure the main form is not created if the user failed to provide the correct password.

Here's how the project's source code should look :

 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.
 

Note the usage of the if then else block to determine if the main form should be created.

If "Execute" returns false, MainForm is not created and the application terminates without starting.