How do I include a password form into my program?

How can I include a password log in form at the start of my program? I need to be able to have several users and be able to change the passwords. Is it possible to have stars coming up on screen instead of the password as you do when you log on to Windows NT?
Thankyou very much.
Chris
chris_emptageAsked:
Who is Participating?
 
westy100697Connect With a Mentor Commented:
chris_emptage,
              There are many ways to include a password log form into the start of your program. I personally use
a database table to store passwords, usernames and levels of access. I also do not allow access to my program unless the user logs in. This is done by making sure you show the logon from through the source of your project as the last form created.

I use this in my project source....

begin
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TSplashScreen,  SplashScreen);
  Application.CreateForm(TDataModule1, DataModule1);
  SplashScreen.Showmodal ;
  LogonForm := TLogonForm.Create(Application) ;
  LogonForm.Showmodal ;

  Application.Run;

end.


For the logon form I just have  a
dbgrid listing the usernames and levels of users, 1 edit box for entering the password and 2 buttons. 1 button is the login button and 1 is a cancel button.

This is the code from my logon form using a database table.....

unit logon;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids,fmMain, Buttons, DosMove;

type
  TLogonForm = class(TForm)
    DBGrid1: TDBGrid;
    Label1: TLabel;
    Password: TEdit;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDeactivate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure PasswordKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  LogonForm: TLogonForm;

implementation
uses
 Splash, dmLogon
{$R *.DFM}
{$OPTIMIZATION OFF}

procedure TLogonForm.FormCreate(Sender: TObject);
begin
  Datamodule2.UserTable.Open ;
  Datamodule3.LogTable.Open ;
end;

procedure TLogonForm.FormActivate(Sender: TObject);
begin
  Datamodule2.UserTable.Active := true ;
  Datamodule3.LogTable.Active := true ;
end;

procedure TLogonForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Datamodule2.UserTable.Close ;
  Datamodule3.LogTable.Close ;
  Release ;
end;

procedure TLogonForm.FormDeactivate(Sender: TObject);
begin
  Datamodule2.UserTable.Active := false ;
  Datamodule3.LogTable.Active := false ;
end;

procedure TLogonForm.BitBtn1Click(Sender: TObject);
var
  TempPass : String ;
begin
  with Datamodule2.UserTable do
  begin
    TempPass := FieldByName('PASSWORD').AsString ;
    if AnsiUpperCase(Password.Text) = FieldByName('PASSWORD').AsString then
    begin
      fmMain.UserName      := FieldByName('NAME').AsString;
      fmMain.UserLevel     := FieldByName('LEVEL').AsInteger;
      fmMain.UserPassword  := FieldByName('PASSWORD').AsString;
      fmMain.UserID        := FieldByName('ID').AsString;
      Password.Text := '';
      LogonForm.close;
    end
    else
    begin
      MessageDlg('Incorrect Password. Please enter password again.',mtError, [mbOK], 0);
      Password.Text := '';
    end;
  end;
  if AnsiUpperCase(Password.Text) = TempPass then
  begin
    Datamodule3.Helpa32LogTable.Append ;
    Datamodule3.LogTable.fieldbyname('THEUSER').AsString := FmMain.UserName;
    Datamodule3.LogTable.fieldbyname('THEDATE').AsString := FormatDateTime('dd/mm/yyyy',Now);
    Datamodule3.LogTable.fieldbyname('THETIME').AsString := FormatDateTime('hh:mm:ss am/pm',Time);
    Datamodule3.Helpa32LogTable.fieldbyname('EVENTS').AsString := 'Logged On to Helpa32';
    Datamodule3.Helpa32LogTable.Post ;
    Datamodule3.Helpa32LogTable.Close ;
  end;
end;

procedure TLogonForm.BitBtn2Click(Sender: TObject);
begin
  Close ;
end;

procedure TLogonForm.DBGrid1CellClick(Column: TColumn);
begin
  Password.SetFocus ;
end;

procedure TLogonForm.PasswordKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  If Key = VK_RETURN then
   BitBtn1Click(Sender);
end;

end.


I hope you may be able to use this. There is already one suggestion here but I thought you may like to see another method.


WESTY :)
0
 
chris_emptageAuthor Commented:
Thankyou very much to anyone who answers this problem of mine.
0
 
jeurkCommented:
for the stars, use the passwordchar property of you field.
Set it to * or ¤ or whatever you like.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
EpsylonCommented:
You need 2 forms: Form1 and Form2. Form2 has 2 editboxes and 2 buttons.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
begin
  if Form2.ShowModal = mrOk then
    ShowMessage('Welcome')
  else begin
    ShowMessage('Login failed');
    Application.Terminate;
  end;
end;

end.



unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm2 = class(TForm)
    Username: TEdit;
    Password: TEdit;
    Login: TButton;
    Abort: TButton;
    procedure LoginClick(Sender: TObject);
    procedure AbortClick(Sender: TObject);
    procedure PasswordKeyPress(Sender: TObject; var Key: Char);
    procedure UsernameKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.LoginClick(Sender: TObject);
const VALIDPASSWORD = 'pass';
begin
  if Password.Text = VALIDPASSWORD then
    ModalResult := mrOk
  else
    ModalResult := mrAbort;
end;

procedure TForm2.AbortClick(Sender: TObject);
begin
  ModalResult := mrAbort
end;

procedure TForm2.PasswordKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    LoginClick(Self);
end;

procedure TForm2.UsernameKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    Password.SetFocus;
end;

end.
0
 
florisbCommented:
in project source:

PasswordDlg := TPasswordDlg.Create(Application);
  PasswordDlg.ShowModal; //set tag to 1 if password is Okee.
  PasswordDlg.Update;
  if passwordDlg.tag = 1 then
     begin
     //password controle Okee.
     Application.CreateForm(TForm1, Form1);
     Application.Run;
     end
  else
    begin
    //password not Okee.
    end;
0
 
chris_emptageAuthor Commented:
Adjusted points from 50 to 70
0
 
chris_emptageAuthor Commented:
Thankyou very much for your answer! It was very great of you to show me this. Unfortuneately though i am not exactly god's gift to Delphi and had trouble understanding what you had written. I was not sure what went where and so I was unable to get it working. But cheers anyway,
Chris
0
 
westy100697Commented:
chris_emptage,
              If you want a more thorough and basic explanantion I can e-mail you something to that effect, only if you want me to??

WESTY :)  
 
0
 
chris_emptageAuthor Commented:
Westy
If you could e-mail me an explanation that would great. My address is chris_emptage@hotmail.com If there is anything I can do for you just let me know. Cheers very very much, I've been having so many problems with this program.
Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.