home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue154 / delphi / password / PW.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1999-04-28  |  2.5 KB  |  86 lines

  1. unit pw;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     Button1: TButton;
  13.     procedure Button1Click(Sender: TObject);
  14.     procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  15.     procedure Edit1KeyDown(Sender: TObject; var Key: Word;
  16.       Shift: TShiftState);
  17.   private
  18.     { Private declarations }
  19.   public
  20.     { Public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.DFM}
  29. const
  30.    password : string = '';
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.    ShowMessage( 'Password = ' + password );
  35. end;
  36.  
  37. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  38. {        Notes:
  39.      1)  the index of the character to be deleted when
  40.          backspace (#8) is pressed is calculated using the
  41.          current cursor pos in Edit1. This, of course,
  42.          requires that the number of characters in Edit1 is
  43.          the same as in password (as it is in this program).
  44.  
  45.      2)  the password is not built up by appending Key to password, e.g.
  46.             password := password + Key;
  47.          Instead a complicated char-insertion routine is used:
  48.             password := Copy(password,1,Edit1.SelStart) +
  49.                 Key +
  50.                 Copy(password,Edit1.SelStart, Length(password)-Edit1.SelStart);
  51.          This is to allow for the possibility that the user may have
  52.          moved the insertion point by clicking the mouse at a new position.
  53. }
  54. begin
  55.   if Key = #8 then // backspace
  56.   begin            // delete char from password matching current cursor pos
  57.      if Length(password) > 0 then // (selStart) in edit box
  58.      begin
  59.         if Edit1.selLength = 0 then
  60.            Delete(password, Edit1.selStart, 1)
  61.         else                     // delete several selected chars if necessary
  62.            Delete(password, Edit1.selStart+1, Edit1.selLength);
  63.      end;
  64.   end
  65.   else
  66.   if Key in ['a'..'z', 'A'..'Z', '0'..'9'] then
  67.   begin     // insert new char at current cursor position
  68.     password := Copy(password,1,Edit1.SelStart) +
  69.                 Key +
  70.                 Copy(password,Edit1.SelStart, Length(password)-Edit1.SelStart);
  71.     Key := '*';
  72.   end
  73.   else Key := #0;
  74.   Caption := password;
  75. end;
  76.  
  77. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  78.   Shift: TShiftState);
  79. begin
  80. { Disallow non-alphanumeric chars such as Home and End }
  81.   if not (Chr(Key) in ['a'..'z', 'A'..'Z', '0'..'9'] ) then
  82.      Key := 0;
  83. end;
  84.  
  85. end.
  86.