Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LOGIN UNIT
- unit UnitLogin;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Main,
- GeneralTopic,
- UnitNewUser;
- type
- TfrmLogin = class(TForm)
- btnLogin: TButton; // btn prefix = button
- lbledtUsername: TLabeledEdit;
- // lbledt = label that data can be inputted into while program is running
- lbledtPassword: TLabeledEdit;
- btnAdd_New_User: TButton;
- lblLoginSuccessful: TLabel; // lbl prefix = label containing data
- btnContinue: TButton;
- lblMessage: TLabel;
- btnLogout: TButton;
- procedure btnLoginClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure btnAdd_New_UserClick(Sender: TObject);
- procedure btnContinueClick(Sender: TObject);
- procedure btnLogoutClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Username, Password: string;
- Userfile: textfile;
- TextString: string;
- end;
- var
- frmLogin: TfrmLogin;
- implementation
- {$R *.dfm}
- procedure TfrmLogin.FormCreate(Sender: TObject);
- begin
- lblMessage.show;
- lbledtUsername.show;
- lbledtPassword.show;
- btnAdd_New_User.show;
- btnLogin.show;
- lblLoginSuccessful.hide;
- btnContinue.hide;
- btnLogout.hide;
- end;
- procedure TfrmLogin.btnAdd_New_UserClick(Sender: TObject);
- begin
- FormNewUser.show; // show new user form
- end;
- procedure TfrmLogin.btnContinueClick(Sender: TObject);
- begin
- FormMainMenu.show; // show quiz menu
- end;
- procedure TfrmLogin.btnLoginClick(Sender: TObject);
- Var
- closing, found, Endendof, incpass: boolean;
- begin
- closing := false;
- found := false;
- Endendof := false;
- incpass := false;
- Username := lbledtUsername.Text;
- Password := lbledtPassword.Text;
- AssignFile(Userfile, 'Usernames.txt'); // open and read Usernames file
- Reset(Userfile);
- while (closing = false) do
- begin
- readln(Userfile, TextString); // read file line by line
- if (TextString = Username) then
- // compare username stored in file to value entered in the username box
- begin
- readln(Userfile, TextString);
- if (TextString = Password) then
- // compare password under that username stored in file to value entered in the password box
- begin
- lbledtUsername.hide; // Hide Username Box
- lbledtPassword.hide; // Hide Password Box
- lblMessage.hide;
- btnLogout.show;
- btnAdd_New_User.hide; // Hide the Button to add new user
- btnLogin.hide; // Hide Button the check login details
- btnContinue.show;
- lblLoginSuccessful.show;
- closing := true;
- found := true;
- end
- else
- begin
- closing := true;
- Showmessage('Please enter your Log In details correctly');
- // Displaying error message
- incpass := true;
- end;
- end;
- end;
- if (found = false) then
- begin
- closing := true;
- if (incpass = false) then
- begin
- Showmessage('Incorrect Username'); //error message for if username is specifically incorrect
- end;
- found := true;
- end;
- closefile(Userfile);
- end;
- procedure TfrmLogin.btnLogoutClick(Sender: TObject);
- // returns to orignal login layout
- begin
- lbledtUsername.show;
- lbledtPassword.show;
- btnAdd_New_User.show;
- btnLogin.show;
- lblLoginSuccessful.hide;
- btnContinue.hide;
- btnLogout.hide;
- lblMessage.show;
- lbledtUsername.Text := ''; //clearing username and password boxes
- lbledtPassword.Text := '';
- end;
- end.
- NEW USER UNIT
- unit UnitNewUser;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
- type
- TFormNewUser = class(TForm)
- lbledtNew_Username: TLabeledEdit;
- lbledtNew_Password: TLabeledEdit;
- btnNew_User: TButton;
- procedure btnNew_UserClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- UserFile: TextFile;
- Textstring: String;
- end;
- var
- FormNewUser: TFormNewUser;
- implementation
- {$R *.dfm}
- procedure TFormNewUser.btnNew_UserClick(Sender: TObject);
- var
- ended, closing, used: boolean;
- usrnm, pswrd: string;
- studentno: integer;
- begin
- usrnm := lbledtNew_Username.Text; // read in username entered in box
- pswrd := lbledtNew_Password.Text; // read in password entered in box
- ended := false;
- closing := false;
- used := false;
- Try
- StrToInt(usrnm);
- except
- On E: EConvertError do
- begin
- Showmessage('Your Username must be your Student Number!');
- ended := true;
- closing := true;
- end;
- End;
- studentno := StrToInt(usrnm);
- if (studentno < 130000) or (studentno >= 170000) then
- begin
- Showmessage('Please enter a valid student number!"');
- ended := true;
- closing := true;
- end;
- if (Length(pswrd) < 6) or (Length(pswrd) > 14) then
- begin
- ended := true;
- closing := true;
- Showmessage('Please enter a Password between 6 and 14 characters!')
- end;
- AssignFile(UserFile, 'Usernames.txt');
- // open usernames folder (usernames file has passwords in it)
- Reset(UserFile);
- while (* not EOF(UserFile) or ( *) ended = false do
- Begin
- Readln(UserFile, Textstring); // checking for duplicate usernames
- If (Textstring = usrnm) then
- begin
- ended := true;
- closing := true;
- used := true;
- Showmessage('Username is taken'); // display error message
- end
- else
- Readln(UserFile, Textstring); // read in username and store it in file
- if EOF(UserFile) then
- begin
- ended := true; // tell program it is at the end of the file
- end;
- End;
- CloseFile(UserFile);
- if (used = false) and (closing = false) then
- // if username has not been used before
- begin
- AssignFile(UserFile, 'Usernames.txt'); // create usernames folder
- Append(UserFile);
- Writeln(UserFile, usrnm); // write username entered to file
- Writeln(UserFile, pswrd); // write password entered to file
- CloseFile(UserFile);
- Showmessage('Succesfully registered!');
- lbledtNew_Username.Text := ''; // clear new username and password box
- lbledtNew_Password.Text := '';
- close;
- end
- { else //if no usernames and passwords have already been asigned
- begin
- lbledtNew_Username.Text := 'Username'; //create default username and password JUSTIFICATION
- lbledtNew_Password.PasswordChar := #0 ;
- lbledtNew_Password.Text := 'Password';
- CloseFile(UserFile); }
- end;
- end.
- MAIN MENU UNIT
- unit Main;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, UnitChoice,
- SpecificTopic1, GeneralTopic;
- type
- TFormMainMenu = class(TForm)
- btnGeneral: TButton;
- btnSpecific: TButton;
- procedure btnSpecificClick(Sender: TObject);
- procedure btnGeneralClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- FormMainMenu: TFormMainMenu;
- implementation
- {$R *.dfm}
- procedure TFormMainMenu.btnGeneralClick(Sender: TObject);
- begin
- GeneralTopic.GeneralTopics.Show;
- close;
- end;
- procedure TFormMainMenu.btnSpecificClick(Sender: TObject);
- begin
- FormTopicChoice.Show // show specific quiz form
- end;
- end.
- GENERAL QUIZ UNIT
- unit GeneralTopic;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
- type
- TGeneralTopics = class(TForm)
- lblQuestion: TLabel;
- lblAnswer1: TLabel;
- lblAnswer2: TLabel;
- lblAnswer3: TLabel;
- lblAnswer4: TLabel;
- Label1: TLabel;
- Label2: TLabel;
- btnSubmit: TButton;
- procedure FormActivate(Sender: TObject);
- procedure lblAnswer1Click(Sender: TObject);
- procedure lblAnswer2Click(Sender: TObject);
- procedure lblAnswer3Click(Sender: TObject);
- procedure lblAnswer4Click(Sender: TObject);
- procedure btnSubmitClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- UserFile, RedoFile: TextFile;
- SecondFile: TextFile;
- Textstring, Redostring, CorrAns, AnsClick: String;
- twoarray: string;
- TimeW: integer;
- end;
- var
- GeneralTopics: TGeneralTopics;
- q: array [1 .. 10] of string; // array of questions
- l: array [1 .. 10] of string;
- // array of the letter coordinates e.g 'A' or 'B' determining if question is about topic A or B
- n: array [1 .. 10] of string;
- // array of the number coordinates e.g 1 - 9 determining which question number is selected from the file
- QuesNum: integer;
- implementation
- {$R *.dfm}
- procedure TGeneralTopics.btnSubmitClick(Sender: TObject);
- Var
- finding, enddd: boolean; //enddd is used as end is not available as a variable name (endd has been used previously)
- tempQN, Quest: string;
- begin
- if (AnsClick = CorrAns) then
- begin
- if (l[QuesNum] = 'A') then
- begin
- Quest := 'Questions.txt';
- end
- else
- begin
- Quest := 'Questions2.txt';
- end;
- AssignFile(UserFile, Quest); // Open the questions file
- Reset(UserFile);
- QuesNum := QuesNum + 1; // increment question number
- tempQN := inttostr(QuesNum);
- // designating the question number of the current question being asked
- finding := true;
- enddd := false;
- if (QuesNum < 10) then
- begin
- tempQN := '0' + tempQN;
- end;
- while finding = true do
- begin
- readln(UserFile, Textstring);
- if (Textstring = n[QuesNum]) then
- // reading through the file till the next question to be asked is found (finding the right line in the file)
- begin
- finding := false;
- end
- else
- begin
- readln(UserFile, Textstring); // reading the question
- readln(UserFile, Textstring); // reading option 1
- readln(UserFile, Textstring); // reading option 2
- readln(UserFile, Textstring); // reading option 3
- readln(UserFile, Textstring); // reading option 4
- readln(UserFile, Textstring); // reading correct answer
- end;
- if EOF(UserFile) then // if its the the end of the file
- begin
- Showmessage('No More Questions');
- // tell the user there are no more questions to be asked
- finding := false;
- enddd := true;
- end;
- end;
- if enddd = false then
- begin
- readln(UserFile, Textstring);
- // reading the questions , options and answers and asigning them to the right captions and labels in the form
- lblQuestion.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer1.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer2.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer3.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer4.Caption := Textstring;
- readln(UserFile, Textstring);
- CorrAns := Textstring;
- closefile(UserFile);
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- AnsClick := 'none';
- Showmessage('Correct Answer');
- lblAnswer1.Font.Color := ClBlack;
- // the labels are black until they are clicked
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end
- else
- begin
- closefile(UserFile);
- end;
- end
- else if (AnsClick = 'none') then
- begin
- Showmessage('Please choose an answer');
- // displaying error message if no answer is clicked before submission
- end
- else
- begin
- Showmessage('Incorrect Answer');
- TimeW := TimeW + 1;
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- // keeping a counter of how many times an incorrect answer is given
- end;
- end;
- procedure TGeneralTopics.FormActivate(Sender: TObject);
- var
- Ranques, RanNum, Redoing, i: integer;
- RanNumSr, Tempo, Tempo2, scource, Quest: string;
- ended, used: boolean;
- begin
- QuesNum := 1;
- TimeW := 0;
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- // keeping a counter of how many times an incorrect answer is given
- AssignFile(UserFile, 'temp.txt');
- Rewrite(UserFile);
- closefile(UserFile);
- ended := false;
- // for i := 1 to 10 do //
- // begin
- Redoing := 10;
- while (Redoing > 0) do
- begin
- //determining random coordinates for a random question to be picked and read out of either file A or B
- ended := false;
- used := true;
- Ranques := Random(2); //creating a random number either 0 or 1
- Ranques := Ranques + 1; //adding 1 for the random number to be either 1 or 2 to choose either file A or B
- RanNum := Random(10); //Choosing integer of question number from that file to be randomly picked
- if Ranques = 1 then
- begin
- twoarray := 'A'; //in this case File A is picked
- end;
- if Ranques = 2 then
- begin
- twoarray := 'B'; //in this case File B is picked
- end;
- RanNumSr := inttostr(RanNum);
- if (RanNum < 10) then
- begin
- RanNumSr := '0' + RanNumSr;
- end;
- twoarray := Concat(twoarray, RanNumSr);
- AssignFile(UserFile, 'temp.txt');
- Reset(UserFile);
- used := false;
- while (* not EOF(UserFile) or ( *) ended = false do
- begin
- readln(UserFile, Textstring);
- if (twoarray = Textstring) then
- begin
- ended := true;
- used := true;
- end;
- if (EOF(UserFile) and (used = false)) then
- begin
- ended := true; // tell program it is at the end of the file
- used := false;
- Redoing := Redoing - 1;
- end;
- end;
- closefile(UserFile);
- if used = false then
- begin
- AssignFile(SecondFile, 'temp.txt');
- Append(SecondFile);
- Writeln(SecondFile, twoarray);
- closefile(SecondFile);
- used := true;
- end;
- end;
- // end; //
- AssignFile(UserFile, 'temp.txt');
- Reset(UserFile);
- for i := 1 to 10 do
- begin
- readln(UserFile, Textstring);
- q[i] := Textstring;
- end;
- closefile(UserFile);
- // Split String //
- for i := 1 to 10 do
- begin
- scource := q[i];
- Tempo := copy(scource, 1, 1);
- l[i] := Tempo;
- Tempo := copy(scource, 2);
- n[i] := Tempo;
- end;
- // String Split //
- if (l[QuesNum] = 'A') then
- begin
- Quest := 'Questions.txt';
- end
- else
- begin
- Quest := 'Questions2.txt';
- end;
- used := false;
- AssignFile(UserFile, Quest);
- Reset(UserFile);
- while (used = false) do
- begin
- readln(UserFile, Textstring);
- if (Textstring = n[QuesNum]) then
- begin
- readln(UserFile, Textstring);
- lblQuestion.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer1.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer2.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer3.Caption := Textstring;
- readln(UserFile, Textstring);
- lblAnswer4.Caption := Textstring;
- readln(UserFile, Textstring);
- CorrAns := Textstring; // determine current correct answer
- used := true;
- end
- else
- used := false;
- end;
- closefile(UserFile);
- end;
- procedure TGeneralTopics.lblAnswer1Click(Sender: TObject);
- begin
- AnsClick := '1';
- lblAnswer1.Font.Color := ClBlue;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TGeneralTopics.lblAnswer2Click(Sender: TObject);
- begin
- AnsClick := '2';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlue;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TGeneralTopics.lblAnswer3Click(Sender: TObject);
- begin
- AnsClick := '3';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlue;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TGeneralTopics.lblAnswer4Click(Sender: TObject);
- begin
- AnsClick := '4';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlue;
- end;
- end.
- SPECIFIC TOPIC CHOICE UNIT
- unit UnitChoice;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SpecificTopic1, SpecificTopic2,
- Vcl.StdCtrls;
- type
- TFormTopicChoice = class(TForm)
- Button1: TButton;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- FormTopicChoice: TFormTopicChoice;
- implementation
- {$R *.dfm}
- procedure TFormTopicChoice.Button1Click(Sender: TObject);
- begin
- FormSpecificTopic1.show; // show specific quiz
- end;
- procedure TFormTopicChoice.Button2Click(Sender: TObject);
- begin
- FormSpecificTopic2.show; // show specific quiz
- end;
- end.
- SPECIFIC TOPIC 1 UNIT
- unit SpecificTopic1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
- type
- TFormSpecificTopic1 = class(TForm)
- lblQuestion: TLabel;
- lblAnswer1: TLabel;
- lblAnswer2: TLabel;
- lblAnswer3: TLabel;
- lblAnswer4: TLabel;
- btnSubmit: TButton;
- Label1: TLabel;
- Label2: TLabel;
- procedure FormActivate(Sender: TObject);
- procedure btnSubmitClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure lblAnswer1Click(Sender: TObject);
- procedure lblAnswer2Click(Sender: TObject);
- procedure lblAnswer3Click(Sender: TObject);
- procedure lblAnswer4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Userfile: textfile;
- TextString, CorrAns, Ansclick: string;
- TimeW: integer;
- end;
- var
- FormSpecificTopic1: TFormSpecificTopic1;
- QuesNum: integer;
- implementation
- {$R *.dfm}
- procedure TFormSpecificTopic1.btnSubmitClick(Sender: TObject);
- Var
- finding, enddd: boolean;
- tempQN: string;
- begin
- if (Ansclick = CorrAns) then
- begin
- AssignFile(Userfile, 'Questions.txt'); // Open the questions file
- Reset(Userfile);
- QuesNum := QuesNum + 1; // increment question number
- tempQN := inttostr(QuesNum);
- // designating the question number of the current question being asked
- finding := true;
- enddd := false;
- if (QuesNum < 10) then
- begin
- tempQN := '0' + tempQN;
- end;
- while finding = true do
- begin
- readln(Userfile, TextString);
- if (TextString = tempQN) then
- // reading through the file till the next question to be asked is found (finding the right line in the file)
- begin
- finding := false;
- end
- else
- begin
- readln(Userfile, TextString); // reading the question
- readln(Userfile, TextString); // reading option 1
- readln(Userfile, TextString); // reading option 2
- readln(Userfile, TextString); // reading option 3
- readln(Userfile, TextString); // reading option 4
- readln(Userfile, TextString); // reading correct answer
- end;
- if EOF(Userfile) then // if its the the end of the file
- begin
- Showmessage('No More Questions');
- // tell the user there are no more questions to be asked
- finding := false;
- enddd := true;
- end;
- end;
- if enddd = false then
- begin
- readln(Userfile, TextString);
- // reading the questions , options and answers and asigning them to the right captions and labels in the form
- lblQuestion.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer1.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer2.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer3.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer4.Caption := TextString;
- readln(Userfile, TextString);
- CorrAns := TextString;
- closefile(Userfile);
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- Ansclick := 'none';
- Showmessage('Correct Answer');
- lblAnswer1.Font.Color := ClBlack;
- // the labels are black until they are clicked
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end
- else
- begin
- closefile(Userfile);
- end;
- end
- else if (Ansclick = 'none') then
- begin
- Showmessage('Please choose an answer');
- // displaying error message if no answer is clicked before submission
- end
- else
- begin
- Showmessage('Incorrect Answer');
- TimeW := TimeW + 1;
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- // keeping a counter of how many times an incorrect answer is given
- end;
- end;
- procedure TFormSpecificTopic1.FormActivate(Sender: TObject);
- begin
- AssignFile(Userfile, 'Questions.txt'); // open questions file
- Reset(Userfile);
- readln(Userfile, TextString);
- QuesNum := strtoint(TextString);
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- // increment question number
- readln(Userfile, TextString);
- lblQuestion.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer1.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer2.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer3.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer4.Caption := TextString;
- readln(Userfile, TextString);
- CorrAns := TextString; // determine current correct answer
- closefile(Userfile);
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic1.FormCreate(Sender: TObject);
- begin
- Ansclick := 'none';
- TimeW := 0;
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- end;
- procedure TFormSpecificTopic1.lblAnswer1Click(Sender: TObject);
- // changing each option to blue once it is clicked
- begin
- Ansclick := '1';
- lblAnswer1.Font.Color := ClBlue;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic1.lblAnswer2Click(Sender: TObject);
- begin
- Ansclick := '2';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlue;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic1.lblAnswer3Click(Sender: TObject);
- begin
- Ansclick := '3';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlue;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic1.lblAnswer4Click(Sender: TObject);
- begin
- Ansclick := '4';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlue;
- end;
- end.
- SPECIFIC TOPIC 2 UNIT
- unit SpecificTopic2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
- type
- TFormSpecificTopic2 = class(TForm)
- lblQuestion: TLabel;
- lblAnswer1: TLabel;
- lblAnswer2: TLabel;
- lblAnswer3: TLabel;
- lblAnswer4: TLabel;
- Label1: TLabel;
- Label2: TLabel;
- btnSubmit: TButton;
- procedure btnSubmitClick(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure lblAnswer1Click(Sender: TObject);
- procedure lblAnswer2Click(Sender: TObject);
- procedure lblAnswer3Click(Sender: TObject);
- procedure lblAnswer4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- Userfile: textfile;
- TextString, CorrAns, Ansclick: string;
- TimeW: integer;
- end;
- var
- FormSpecificTopic2: TFormSpecificTopic2;
- QuesNum: integer;
- implementation
- {$R *.dfm}
- procedure TFormSpecificTopic2.btnSubmitClick(Sender: TObject);
- Var
- finding, enddd: boolean;
- tempQN: string;
- begin
- if (Ansclick = CorrAns) then
- begin
- AssignFile(Userfile, 'Questions2.txt'); // Open the questions file
- Reset(Userfile);
- QuesNum := QuesNum + 1; // increment question number
- tempQN := inttostr(QuesNum);
- // designating the question number of the current question being asked
- finding := true;
- enddd := false;
- if (QuesNum < 10) then
- begin
- tempQN := '0' + tempQN;
- end;
- while finding = true do
- begin
- readln(Userfile, TextString);
- if (TextString = tempQN) then
- // reading through the file till the next question to be asked is found (finding the right line in the file)
- begin
- finding := false;
- end
- else
- begin
- readln(Userfile, TextString); // reading the question
- readln(Userfile, TextString); // reading option 1
- readln(Userfile, TextString); // reading option 2
- readln(Userfile, TextString); // reading option 3
- readln(Userfile, TextString); // reading option 4
- readln(Userfile, TextString); // reading correct answer
- end;
- if EOF(Userfile) then // if it’s the end of the file
- begin
- Showmessage('No More Questions');
- // tell the user there are no more questions to be asked
- finding := false;
- enddd := true;
- end;
- end;
- if enddd = false then
- begin
- readln(Userfile, TextString);
- // reading the questions , options and answers and asigning them to the right captions and labels in the form
- lblQuestion.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer1.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer2.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer3.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer4.Caption := TextString;
- readln(Userfile, TextString);
- CorrAns := TextString;
- closefile(Userfile);
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- Ansclick := 'none';
- Showmessage('Correct Answer');
- lblAnswer1.Font.Color := ClBlack;
- // the labels are black until they are clicked
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end
- else
- begin
- closefile(Userfile);
- end;
- end
- else if (Ansclick = 'none') then
- begin
- Showmessage('Please choose an answer');
- // displaying error message if no answer is clicked before submission
- end
- else
- begin
- Showmessage('Incorrect Answer');
- TimeW := TimeW + 1;
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- // keeping a counter of how many times an incorrect answer is given
- end;
- end;
- procedure TFormSpecificTopic2.FormActivate(Sender: TObject);
- begin
- AssignFile(Userfile, 'Questions2.txt'); // open questions file
- Reset(Userfile);
- readln(Userfile, TextString);
- QuesNum := strtoint(TextString);
- Label1.Caption := ('Question Number ' + inttostr(QuesNum));
- // increment question number
- readln(Userfile, TextString);
- lblQuestion.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer1.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer2.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer3.Caption := TextString;
- readln(Userfile, TextString);
- lblAnswer4.Caption := TextString;
- readln(Userfile, TextString);
- CorrAns := TextString; // determine current correct answer
- closefile(Userfile);
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic2.FormCreate(Sender: TObject);
- begin
- Ansclick := 'none';
- TimeW := 0;
- Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
- end;
- procedure TFormSpecificTopic2.lblAnswer1Click(Sender: TObject);
- begin
- Ansclick := '1';
- lblAnswer1.Font.Color := ClBlue;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic2.lblAnswer2Click(Sender: TObject);
- begin
- Ansclick := '2';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlue;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic2.lblAnswer3Click(Sender: TObject);
- begin
- Ansclick := '3';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlue;
- lblAnswer4.Font.Color := ClBlack;
- end;
- procedure TFormSpecificTopic2.lblAnswer4Click(Sender: TObject);
- begin
- Ansclick := '4';
- lblAnswer1.Font.Color := ClBlack;
- lblAnswer2.Font.Color := ClBlack;
- lblAnswer3.Font.Color := ClBlack;
- lblAnswer4.Font.Color := ClBlue;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement