Require a password login into your Delphi Application

By | March 15, 2011
Selec All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 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.

Selec All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 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.