Advertisement
MitulC

Untitled

Apr 2nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.73 KB | None | 0 0
  1. unit Signup;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.   System.Classes, Vcl.Graphics,
  8.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  9.  
  10. type
  11.  
  12.   TUserInfo = Record
  13.     Username: String[25];
  14.     Password: String[25];
  15.   End;
  16.  
  17.   TfrmSignup = class(TForm)
  18.     lblEnterDetails: TLabel;
  19.     lblUsername: TLabel;
  20.     edtUsername: TEdit;
  21.     lblPassword: TLabel;
  22.     edtPassword: TEdit;
  23.     lblPassword2: TLabel;
  24.     edtPassword2: TEdit;
  25.     btnSignUp: TButton;
  26.     btnExit: TButton;
  27.     lblPasswordMatch: TLabel;
  28.     lblUserTaken: TLabel;
  29.     edtTesting: TEdit;
  30.     procedure btnExitClick(Sender: TObject);
  31.     procedure btnSignUpClick(Sender: TObject);
  32.     procedure edtPasswordKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  33.     procedure edtPassword2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  34.     procedure edtUsernameKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  35.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  36.  
  37.   private
  38.     { Private declarations }
  39.   public
  40.     Info : File of TUserInfo;    //Record for user data
  41.   end;
  42.  
  43. var
  44.  
  45.   Info: File of TUserInfo;
  46.   frmSignup: TfrmSignup;
  47.   UsernameInput, PasswordInput : string[25];
  48.   UserInfo: TUserInfo;
  49.  
  50. implementation
  51.  
  52. {$R *.dfm}
  53.  
  54. uses Login, Main;
  55.  
  56. procedure CheckFieldsAreComplete;
  57. begin
  58.   if (frmSignup.lblPasswordMatch.Caption = 'Passwords Match') and (frmSignup.edtUsername.Text <> '') and
  59.     (frmSignup.edtPassword.Text <> '') and (frmSignup.edtPassword2.Text <> '') and
  60.     not(frmSignup.lblUserTaken.visible) then   //Checks that all the fields are completed
  61.                                      //for signup
  62.     frmSignup.btnSignup.Enabled := True;
  63. end;
  64.  
  65. procedure TfrmSignup.btnSignUpClick(Sender: TObject);
  66. begin
  67.   UsernameInput := edtUsername.Text;
  68.   PasswordInput := edtPassword.Text;
  69.   frmSignup.visible := false;
  70.   frmMainMenu.visible := True;
  71.   edtUsername.Text := '';     //makes all fields empty when sign up is pressed
  72.   edtPassword.Text := '';
  73.   edtPassword2.Text := '';
  74. {$I-}
  75.   AssignFile(Info, 'UserInfo.dat');
  76.   reset(Info)   ;
  77. {$I+}
  78.   if (IOResult <> 0) then
  79.   begin                                 //Checks if the file exists when the user clicks the signup button. If it doesn't then recreate the file
  80.     rewrite(Info);
  81.     Closefile(Info);
  82.   end;
  83.   AssignFile(Info, 'UserInfo.dat');
  84.   reset (Info);
  85.   UserInfo.Username := edtUsername.Text;
  86.   UserInfo.Password := edtPassword.Text;
  87.   Seek(Info, Filesize(Info));
  88.   Write(Info, UserInfo);
  89.   Closefile(Info);
  90.   edtTesting.Text := 'written to file';
  91. end;
  92.  
  93. procedure TfrmSignup.btnExitClick(Sender: TObject);
  94. begin
  95.   frmSignup.visible := false;
  96.   frmMainMenu.visible := True;     //Show main form
  97. end;
  98.  
  99. procedure TfrmSignup.edtUsernameKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  100. var              //Checks to see if the username is taken
  101.   UserTaken: Boolean;
  102. begin
  103.   UserTaken := True;
  104.   AssignFile(Info, 'UserInfo.dat');
  105.   reset(Info);
  106.   while Not Eof(Info) do
  107.   begin
  108.     read(Info, UserInfo);
  109.     if edtUsername.Text = UserInfo.Username then  //If username entered matches one in the file then...
  110.     begin
  111.       lblUserTaken.visible := True;  //Notify the user
  112.     end
  113.     else
  114.     begin
  115.       UserTaken := false;
  116.       lblUserTaken.visible := false;
  117.     end;
  118.     if Not UserTaken then
  119.     begin
  120.       CheckFieldsAreComplete;
  121.       break
  122.     end;
  123.   end;
  124.   Closefile(Info)
  125.  
  126. end;
  127.  
  128. procedure TfrmSignup.edtPasswordKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  129. begin
  130.   begin            //Check if passwords match
  131.     frmSignup.lblPasswordMatch.visible := True;
  132.     if (edtPassword.Text <> edtPassword2.Text) then
  133.       frmSignup.lblPasswordMatch.Caption := 'Passwords do not match';
  134.     if (edtPassword.Text <> edtPassword2.Text) then
  135.       frmSignup.lblPasswordMatch.Color := clgreen
  136.     else
  137.     begin
  138.       lblPasswordMatch.Caption := 'Passwords Match';
  139.       lblPasswordMatch.Color := clRed
  140.     end;
  141.     CheckFieldsAreComplete;
  142.   end;
  143. end;
  144.  
  145. procedure TfrmSignup.edtPassword2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  146. begin                                //Check if passwords match
  147.   lblPasswordMatch.visible := True;
  148.   if edtPassword.Text <> edtPassword2.Text then
  149.     lblPasswordMatch.Caption := 'Passwords do not match';
  150.   if edtPassword.Text <> edtPassword2.Text then
  151.     lblPasswordMatch.Color := clRed
  152.   else
  153.   begin
  154.     lblPasswordMatch.Caption := 'Password Match';
  155.     lblPasswordMatch.Font.Color := clgreen;
  156.   end;
  157.   CheckFieldsAreComplete;
  158. end;
  159.  
  160. procedure TfrmSignup.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  161. begin
  162. frmSignUp.Visible := False;
  163. frmMainMenu.Visible := true;
  164. end;
  165.  
  166. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement