Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit LoginUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Mask,
- Vcl.Imaging.pngimage;
- type
- TfrmLogin = class(TForm)
- btnEnter: TButton;
- ledtUsername: TLabeledEdit;
- btnExit: TButton;
- medtPassword: TMaskEdit;
- lblPassword: TLabel;
- imgLogo: TImage;
- procedure btnEnterClick(Sender: TObject);
- procedure btnExitClick(Sender: TObject);
- //record for username and password
- Type TCredentials = Record
- Username : String[15];
- Password : String[20];
- UserType : String[7];
- End;
- private
- { Private declarations }
- public
- { Public declarations }
- UserArray : array [1..500] of TCredentials;//array storing sorted usernames + passwords
- i : integer;
- end;
- var
- frmLogin: TfrmLogin;
- implementation
- {$R *.dfm}
- Uses
- TeacherMenuUnit , StudentMenuUnit , TeachersReportUnit;
- procedure TfrmLogin.btnEnterClick(Sender: TObject);
- var
- FileArray : array [1..500] of TCredentials; //Stores content from file in separate indexes
- LoginFile : TextFile;
- userptr , fileptr , counter : Integer;
- credentials : String;
- TeacherValid , StudentValid : Boolean;
- begin
- //storing usernames and passwords into array 'FileArray' in separate indexes:
- i := 1;
- assignfile(LoginFile , 'LoginDetails.txt');
- Reset(LoginFile);
- while not EOF(LoginFile) do
- Begin
- readln(LoginFile , credentials); //store username/password from file
- FileArray[i].Username := credentials; //write username/password to array
- i := i + 1; //increment array index by one
- End;
- CloseFile(LoginFile);
- //store username with coresponding passwords in the same index in array 'UserArray'
- userptr := 1;
- fileptr := 1;
- i := i DIV 3; //makes sure no extra array indexes are used
- for counter := 1 to i do
- Begin
- //store username in one index:
- UserArray[userptr].Username := FileArray[fileptr].Username;
- //store password in same index:
- UserArray[userptr].Password := FileArray[fileptr + 1].Username;
- //store user type in same index:
- UserArray[userptr].UserType := FileArray[fileptr + 2].Username;
- userptr := userptr + 1; //increment userarray by one
- fileptr := fileptr + 3 //increment filearray by two (goes to next username)
- End;
- //Search for username and password in array
- TeacherValid := False;
- StudentValid := False;
- for counter := 1 to i do
- Begin
- //comparing username and password to array
- //teacher login prerequisites
- if (UserArray[counter].Username = ledtUsername.Text)
- AND (UserArray[counter].Password = medtPassword.Text)
- AND (UserArray[counter].UserType = 'Teacher') then
- Begin
- TeacherValid := True;
- End;
- //student login prerequisites
- if (UserArray[counter].Username = ledtUsername.Text)
- AND (UserArray[counter].Password = medtPassword.Text)
- AND (UserArray[counter].UserType = 'Student') then
- Begin
- StudentValid := True;
- End;
- End;
- //Teacher logging in
- if TeacherValid = True then
- Begin
- ShowMessage('Login Successful');
- frmTeacherMenu.Show; //Display Teacher's Menu
- //display welcome message:
- frmTeacherMenu.lblWelcome.Caption := 'Hello ' + ledtUsername.Text;
- End
- //Student logging in
- Else if StudentValid = True then
- Begin
- ShowMessage('Login Successful');
- frmStudentMenu.Show; //Display Student's Menu
- //display welcome message:
- frmStudentMenu.lblWelcome.Caption := 'Hello ' + ledtUsername.Text;
- End
- //when both username and password left blank:
- Else if (medtPassword.Text = '') AND (ledtUsername.Text = '') then
- ShowMessage('Please enter a username and password before logging in')
- //when no username entered
- Else if ledtUsername.Text = '' then
- ShowMessage('Please enter a username')
- //when no password entered
- Else if medtPassword.Text = '' then
- ShowMessage('Please enter a Password')
- //incorrect login details
- Else
- Begin
- //create custom messagebox
- IF Application.Messagebox('Incorrect Login Details, Please Try Again',
- 'Incorrect',
- MB_RetryCancel or MB_IconError) = IDCancel then
- Begin
- Close; //closes the form when 'Cancel' is clicked in messagebox
- End
- ELSE
- Begin
- ledtUsername.Text := ''; //clears username
- medtPassword.Text := ''; //clears password
- End;
- End;
- End;
- procedure TfrmLogin.btnExitClick(Sender: TObject);
- begin
- frmLogin.Close; //closes login form
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement