Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.82 KB | None | 0 0
  1. unit UnitLogin;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Main, UnitNewUser;
  8.  
  9. type
  10.   TfrmLogin = class(TForm)
  11.     btnLogin: TButton;      // btn prefix = button
  12.     lbledtUsername: TLabeledEdit; // lbledt = label that data can be inputted into while program is running
  13.     lbledtPassword: TLabeledEdit;
  14.     btnAdd_New_User: TButton;
  15.     lblLoginSuccessful: TLabel; // lbl prefix = label containing data
  16.     btnContinue: TButton;
  17.     lblMessage: TLabel;
  18.     btnLogout: TButton;
  19.     procedure btnLoginClick(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure btnAdd_New_UserClick(Sender: TObject);
  22.     procedure btnContinueClick(Sender: TObject);
  23.     procedure btnLogoutClick(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.   public
  27.     { Public declarations }
  28.     Username,Password : string;
  29.     Userfile : textfile;
  30.     TextString : string;
  31.   end;
  32.  
  33. var
  34.   frmLogin: TfrmLogin;
  35.  
  36.  
  37. implementation
  38. {$R *.dfm}
  39.  
  40. procedure TfrmLogin.FormCreate(Sender: TObject);
  41. begin
  42.   lblMessage.show;
  43.   lbledtUsername.show;
  44.   lbledtPassword.show;
  45.   btnAdd_New_user.show;
  46.   btnLogin.show;
  47.  
  48.   lblLoginSuccessful.hide;
  49.   btnContinue.hide;
  50.   btnlogout.hide;
  51. end;
  52.  
  53. procedure TfrmLogin.btnAdd_New_UserClick(Sender: TObject);
  54. begin
  55.   FormNewUser.Show;   //show new user form
  56. end;
  57.  
  58. procedure TfrmLogin.btnContinueClick(Sender: TObject);
  59. begin
  60.    FormMainMenu.Show;    //show quiz menu
  61. end;
  62.  
  63. procedure TfrmLogin.btnLoginClick(Sender: TObject);
  64. Var closing,found, Endendof,incpass : boolean;
  65. begin
  66.   Closing := false;
  67.   Found := false;
  68.   endendof := false;
  69.   incPass:= false;
  70.   Username := lbledtUsername.Text;
  71.   Password := lbledtPassword.Text;
  72.  
  73.   AssignFile(Userfile, 'Usernames.txt') ;   //open and read Usernames file
  74.   Reset(Userfile);
  75.   while (Closing = false) do
  76.   begin
  77.   readln(Userfile,textstring);     //read file line by line
  78.   if (textstring = Username) then  //compare username stored in file to value entered in the username box
  79.   begin
  80.     readln(Userfile,textstring);
  81.     if (textstring = password) then  //compare password under that username stored in file to value entered in the password box
  82.     begin
  83.       lbledtUsername.Hide;     //Hide Username Box
  84.       lbledtPassword.Hide;     //Hide Password Box
  85.       lblMessage.Hide;
  86.       btnLogOut.Show;
  87.       btnAdd_New_user.Hide;    //Hide the Button to add new user
  88.       btnLogin.Hide;           //Hide Button the check login details
  89.       btnContinue.Show;
  90.       lblLoginSuccessful.Show;
  91.       closing := true;
  92.       found := true;
  93.     end
  94. else
  95.  begin
  96.     closing := true;
  97.       Showmessage('Incorrect Password');     //Displaying error message
  98.       incPass:=true;
  99.     end;
  100.   end;
  101.  
  102.   end;
  103.  
  104.   if (Found = false) then
  105.   begin
  106.      closing := true;
  107.     if (incpass=false) then
  108.     begin
  109.     Showmessage('Incorrect Username');
  110.     end;
  111.     Found := true;
  112.   end;
  113.  
  114.  
  115.   closefile(UserFile);
  116. end;
  117.  
  118.  
  119.  
  120. procedure TfrmLogin.btnLogoutClick(Sender: TObject);  //returns to orignal login layout
  121. begin
  122.     lbledtUsername.Show;
  123.       lbledtPassword.show;
  124.       btnAdd_New_user.show;
  125.       btnLogin.show;
  126.       lblLoginSuccessful.hide;
  127.       btnContinue.Hide;
  128.       btnlogout.Hide;
  129.       lblMessage.Show;
  130.       lbledtUsername.Text:='';
  131.       lbledtPassword.Text:='';
  132. end;
  133.  
  134. end.
  135.  
  136. NEW USER Module
  137.  
  138. unit UnitNewUser;
  139.  
  140. interface
  141.  
  142. uses
  143.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  144.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
  145.  
  146. type
  147.   TFormNewUser = class(TForm)
  148.     lbledtNew_Username: TLabeledEdit;
  149.     lbledtNew_Password: TLabeledEdit;
  150.     btnNew_User: TButton;
  151.     procedure btnNew_UserClick(Sender: TObject);
  152.  
  153.   private
  154.     { Private declarations }
  155.   public
  156.     { Public declarations }
  157.      UserFile : TextFile  ;
  158.      Textstring: String;
  159.   end;
  160.  
  161. var
  162.   FormNewUser: TFormNewUser;
  163.  
  164. implementation
  165.  
  166. {$R *.dfm}
  167.  
  168.  
  169. procedure TFormNewUser.btnNew_UserClick(Sender: TObject);
  170. var ended, used: boolean;
  171.     usrnm, pswrd: string;
  172.  
  173. begin
  174. usrnm:= lbledtNew_Username.Text;         //read in username entered in box
  175. pswrd:= lbledtNew_Password.Text;         //read in password entered in box
  176. ended:=false;
  177.   Used:=False;
  178.   AssignFile(UserFile, 'Usernames.txt'); //open usernames folder (usernames file has passwords in it)
  179.   {$I-}
  180.   Reset(UserFile);
  181.   {$i+}
  182.   while (* not EOF(UserFile) or ( *) ended=false do
  183.   Begin
  184.       Readln(UserFile, TextString);    //checking for duplicate usernames
  185.       If (Textstring = Usrnm) then
  186.       begin
  187.          ended:=true;
  188.          Used:=True;
  189.            Showmessage('Username is taken');  //display error message
  190.       end
  191.       else
  192.  
  193.       Readln(UserFile, TextString);         //read in username and store it in file
  194.           if EOF(UserFile) then
  195.       begin
  196.       ended := True;                       //tell program it is at the end of the file
  197.       end;
  198.  
  199.   End;
  200.    CloseFile(UserFile);
  201.  
  202.   if (Used=false) then                         //if username has not been used before
  203.   begin
  204.   AssignFile (UserFile, 'Usernames.txt');      //create usernames folder
  205.   Append(UserFile);
  206.   Writeln(UserFile, UsrNm);                    //write username entered to file
  207.   Writeln(UserFile, Pswrd);                    //write password entered to file
  208.   CloseFile(UserFile);
  209. Showmessage('Succesfully registered!');
  210. close;
  211.    end
  212.    
  213. else                                       //if no usernames and passwords have already been asigned
  214.    begin
  215.      lbledtNew_Username.Text := 'Username';   //create default username and password
  216.  
  217.      lbledtNew_Password.PasswordChar := #0 ;
  218.      lbledtNew_Password.Text := 'Password';
  219.      CloseFile(UserFile);
  220.  
  221.    end
  222. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement