Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 29.44 KB | None | 0 0
  1. LOGIN UNIT
  2. unit UnitLogin;
  3.  
  4. interface
  5.  
  6. uses
  7.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  8.   System.Classes, Vcl.Graphics,
  9.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Main,
  10.   GeneralTopic,
  11.   UnitNewUser;
  12.  
  13. type
  14.   TfrmLogin = class(TForm)
  15.     btnLogin: TButton; // btn prefix = button
  16.     lbledtUsername: TLabeledEdit;
  17.     // lbledt = label that data can be inputted into while program is running
  18.     lbledtPassword: TLabeledEdit;
  19.     btnAdd_New_User: TButton;
  20.     lblLoginSuccessful: TLabel; // lbl prefix = label containing data
  21.     btnContinue: TButton;
  22.     lblMessage: TLabel;
  23.     btnLogout: TButton;
  24.     procedure btnLoginClick(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure btnAdd_New_UserClick(Sender: TObject);
  27.     procedure btnContinueClick(Sender: TObject);
  28.     procedure btnLogoutClick(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.   public
  32.     { Public declarations }
  33.     Username, Password: string;
  34.     Userfile: textfile;
  35.     TextString: string;
  36.   end;
  37.  
  38. var
  39.   frmLogin: TfrmLogin;
  40.  
  41. implementation
  42.  
  43. {$R *.dfm}
  44.  
  45. procedure TfrmLogin.FormCreate(Sender: TObject);
  46. begin
  47.  
  48.   lblMessage.show;
  49.   lbledtUsername.show;
  50.   lbledtPassword.show;
  51.   btnAdd_New_User.show;
  52.   btnLogin.show;
  53.  
  54.   lblLoginSuccessful.hide;
  55.   btnContinue.hide;
  56.   btnLogout.hide;
  57. end;
  58.  
  59. procedure TfrmLogin.btnAdd_New_UserClick(Sender: TObject);
  60. begin
  61.   FormNewUser.show; // show new user form
  62. end;
  63.  
  64. procedure TfrmLogin.btnContinueClick(Sender: TObject);
  65. begin
  66.   FormMainMenu.show; // show quiz menu
  67. end;
  68.  
  69. procedure TfrmLogin.btnLoginClick(Sender: TObject);
  70. Var
  71.   closing, found, Endendof, incpass: boolean;
  72. begin
  73.   closing := false;
  74.   found := false;
  75.   Endendof := false;
  76.   incpass := false;
  77.   Username := lbledtUsername.Text;
  78.   Password := lbledtPassword.Text;
  79.  
  80.   AssignFile(Userfile, 'Usernames.txt'); // open and read Usernames file
  81.   Reset(Userfile);
  82.   while (closing = false) do
  83.   begin
  84.     readln(Userfile, TextString); // read file line by line
  85.     if (TextString = Username) then
  86.     // compare username stored in file to value entered in the username box
  87.     begin
  88.  
  89.       readln(Userfile, TextString);
  90.       if (TextString = Password) then
  91.       // compare password under that username stored in file to value entered in the password box
  92.       begin
  93.         lbledtUsername.hide; // Hide Username Box
  94.         lbledtPassword.hide; // Hide Password Box
  95.         lblMessage.hide;
  96.         btnLogout.show;
  97.         btnAdd_New_User.hide; // Hide the Button to add new user
  98.         btnLogin.hide; // Hide Button the check login details
  99.         btnContinue.show;
  100.         lblLoginSuccessful.show;
  101.         closing := true;
  102.         found := true;
  103.       end
  104.       else
  105.       begin
  106.         closing := true;
  107.         Showmessage('Please enter your Log In details correctly');
  108.         // Displaying error message
  109.         incpass := true;
  110.       end;
  111.  
  112.     end;
  113.  
  114.   end;
  115.  
  116.   if (found = false) then
  117.   begin
  118.     closing := true;
  119.  
  120.     if (incpass = false) then
  121.     begin
  122.       Showmessage('Incorrect Username'); //error message for if username is specifically incorrect
  123.     end;
  124.     found := true;
  125.   end;
  126.  
  127.   closefile(Userfile);
  128. end;
  129.  
  130. procedure TfrmLogin.btnLogoutClick(Sender: TObject);
  131. // returns to orignal login layout
  132. begin
  133.   lbledtUsername.show;
  134.   lbledtPassword.show;
  135.   btnAdd_New_User.show;
  136.   btnLogin.show;
  137.   lblLoginSuccessful.hide;
  138.   btnContinue.hide;
  139.   btnLogout.hide;
  140.   lblMessage.show;
  141.   lbledtUsername.Text := '';  //clearing username and password boxes
  142.   lbledtPassword.Text := '';
  143. end;
  144.  
  145. end.
  146.  
  147. NEW USER UNIT
  148.  
  149. unit UnitNewUser;
  150.  
  151. interface
  152.  
  153. uses
  154.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  155.   System.Classes, Vcl.Graphics,
  156.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
  157.  
  158. type
  159.   TFormNewUser = class(TForm)
  160.     lbledtNew_Username: TLabeledEdit;
  161.     lbledtNew_Password: TLabeledEdit;
  162.     btnNew_User: TButton;
  163.     procedure btnNew_UserClick(Sender: TObject);
  164.  
  165.   private
  166.     { Private declarations }
  167.   public
  168.     { Public declarations }
  169.     UserFile: TextFile;
  170.     Textstring: String;
  171.   end;
  172.  
  173. var
  174.   FormNewUser: TFormNewUser;
  175.  
  176. implementation
  177.  
  178. {$R *.dfm}
  179.  
  180. procedure TFormNewUser.btnNew_UserClick(Sender: TObject);
  181. var
  182.   ended, closing, used: boolean;
  183.   usrnm, pswrd: string;
  184.   studentno: integer;
  185.  
  186. begin
  187.   usrnm := lbledtNew_Username.Text; // read in username entered in box
  188.   pswrd := lbledtNew_Password.Text; // read in password entered in box
  189.   ended := false;
  190.   closing := false;
  191.   used := false;
  192.   Try
  193.     StrToInt(usrnm);
  194.   except
  195.     On E: EConvertError do
  196.     begin
  197.       Showmessage('Your Username must be your Student Number!');
  198.       ended := true;
  199.       closing := true;
  200.     end;
  201.  
  202.   End;
  203.  
  204.   studentno := StrToInt(usrnm);
  205.   if (studentno < 130000) or (studentno >= 170000) then
  206.   begin
  207.     Showmessage('Please enter a valid student number!"');
  208.     ended := true;
  209.     closing := true;
  210.   end;
  211.   if (Length(pswrd) < 6) or (Length(pswrd) > 14) then
  212.   begin
  213.     ended := true;
  214.     closing := true;
  215.     Showmessage('Please enter a Password between 6 and 14 characters!')
  216.   end;
  217.  
  218.   AssignFile(UserFile, 'Usernames.txt');
  219.   // open usernames folder (usernames file has passwords in it)
  220.  
  221.   Reset(UserFile);
  222.  
  223.   while (* not EOF(UserFile) or ( *) ended = false do
  224.   Begin
  225.     Readln(UserFile, Textstring); // checking for duplicate usernames
  226.  
  227.     If (Textstring = usrnm) then
  228.     begin
  229.       ended := true;
  230.       closing := true;
  231.       used := true;
  232.       Showmessage('Username is taken'); // display error message
  233.     end
  234.     else
  235.  
  236.       Readln(UserFile, Textstring); // read in username and store it in file
  237.     if EOF(UserFile) then
  238.     begin
  239.       ended := true; // tell program it is at the end of the file
  240.     end;
  241.  
  242.   End;
  243.   CloseFile(UserFile);
  244.  
  245.   if (used = false) and (closing = false) then
  246.   // if username has not been used before
  247.   begin
  248.     AssignFile(UserFile, 'Usernames.txt'); // create usernames folder
  249.     Append(UserFile);
  250.     Writeln(UserFile, usrnm); // write username entered to file
  251.     Writeln(UserFile, pswrd); // write password entered to file
  252.     CloseFile(UserFile);
  253.     Showmessage('Succesfully registered!');
  254.     lbledtNew_Username.Text := ''; // clear new username and password box
  255.     lbledtNew_Password.Text := '';
  256.     close;
  257.   end
  258.   { else                                       //if no usernames and passwords have already been asigned
  259.     begin
  260.     lbledtNew_Username.Text := 'Username';   //create default username and password   JUSTIFICATION
  261.  
  262.     lbledtNew_Password.PasswordChar := #0 ;
  263.     lbledtNew_Password.Text := 'Password';
  264.     CloseFile(UserFile); }
  265.  
  266. end;
  267.  
  268. end.
  269.  
  270. MAIN MENU UNIT
  271.  
  272. unit Main;
  273.  
  274. interface
  275.  
  276. uses
  277.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  278.   System.Classes, Vcl.Graphics,
  279.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, UnitChoice,
  280.   SpecificTopic1, GeneralTopic;
  281.  
  282. type
  283.   TFormMainMenu = class(TForm)
  284.     btnGeneral: TButton;
  285.     btnSpecific: TButton;
  286.     procedure btnSpecificClick(Sender: TObject);
  287.     procedure btnGeneralClick(Sender: TObject);
  288.   private
  289.     { Private declarations }
  290.   public
  291.     { Public declarations }
  292.   end;
  293.  
  294. var
  295.   FormMainMenu: TFormMainMenu;
  296.  
  297. implementation
  298.  
  299. {$R *.dfm}
  300.  
  301. procedure TFormMainMenu.btnGeneralClick(Sender: TObject);
  302. begin
  303.   GeneralTopic.GeneralTopics.Show;
  304.   close;
  305. end;
  306.  
  307. procedure TFormMainMenu.btnSpecificClick(Sender: TObject);
  308. begin
  309.   FormTopicChoice.Show // show specific quiz form
  310. end;
  311.  
  312. end.
  313.  
  314. GENERAL QUIZ UNIT
  315. unit GeneralTopic;
  316.  
  317. interface
  318.  
  319. uses
  320.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  321.   System.Classes, Vcl.Graphics,
  322.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  323.  
  324. type
  325.   TGeneralTopics = class(TForm)
  326.     lblQuestion: TLabel;
  327.     lblAnswer1: TLabel;
  328.     lblAnswer2: TLabel;
  329.     lblAnswer3: TLabel;
  330.     lblAnswer4: TLabel;
  331.     Label1: TLabel;
  332.     Label2: TLabel;
  333.     btnSubmit: TButton;
  334.     procedure FormActivate(Sender: TObject);
  335.     procedure lblAnswer1Click(Sender: TObject);
  336.     procedure lblAnswer2Click(Sender: TObject);
  337.     procedure lblAnswer3Click(Sender: TObject);
  338.     procedure lblAnswer4Click(Sender: TObject);
  339.     procedure btnSubmitClick(Sender: TObject);
  340.   private
  341.     { Private declarations }
  342.   public
  343.     { Public declarations }
  344.     UserFile, RedoFile: TextFile;
  345.     SecondFile: TextFile;
  346.     Textstring, Redostring, CorrAns, AnsClick: String;
  347.     twoarray: string;
  348.     TimeW: integer;
  349.   end;
  350.  
  351. var
  352.   GeneralTopics: TGeneralTopics;
  353.   q: array [1 .. 10] of string; // array of questions
  354.   l: array [1 .. 10] of string;
  355.   // array of the letter coordinates  e.g 'A' or 'B' determining if question is about topic A or B
  356.   n: array [1 .. 10] of string;
  357.   // array of the number coordinates  e.g 1 - 9 determining which question number is selected from the file
  358.   QuesNum: integer;
  359.  
  360. implementation
  361.  
  362. {$R *.dfm}
  363.  
  364. procedure TGeneralTopics.btnSubmitClick(Sender: TObject);
  365. Var
  366.   finding, enddd: boolean;  //enddd is used as end is not available as a variable name (endd has been used previously)
  367.   tempQN, Quest: string;
  368. begin
  369.   if (AnsClick = CorrAns) then
  370.   begin
  371.     if (l[QuesNum] = 'A') then
  372.     begin
  373.       Quest := 'Questions.txt';
  374.     end
  375.     else
  376.     begin
  377.       Quest := 'Questions2.txt';
  378.     end;
  379.  
  380.     AssignFile(UserFile, Quest); // Open the questions file
  381.     Reset(UserFile);
  382.     QuesNum := QuesNum + 1; // increment question number
  383.     tempQN := inttostr(QuesNum);
  384.     // designating the question number of the current question being asked
  385.     finding := true;
  386.     enddd := false;
  387.  
  388.     if (QuesNum < 10) then
  389.     begin
  390.       tempQN := '0' + tempQN;
  391.     end;
  392.  
  393.     while finding = true do
  394.     begin
  395.       readln(UserFile, Textstring);
  396.       if (Textstring = n[QuesNum]) then
  397.       // reading through the file till the next question to be asked is found (finding the right line in the file)
  398.       begin
  399.         finding := false;
  400.       end
  401.       else
  402.       begin
  403.         readln(UserFile, Textstring); // reading the question
  404.         readln(UserFile, Textstring); // reading option 1
  405.         readln(UserFile, Textstring); // reading option 2
  406.         readln(UserFile, Textstring); // reading option 3
  407.         readln(UserFile, Textstring); // reading option 4
  408.         readln(UserFile, Textstring); // reading correct answer
  409.       end;
  410.  
  411.       if EOF(UserFile) then // if its the the end of the file
  412.       begin
  413.         Showmessage('No More Questions');
  414.         // tell the user there are no more questions to be asked
  415.         finding := false;
  416.         enddd := true;
  417.       end;
  418.  
  419.     end;
  420.     if enddd = false then
  421.     begin
  422.       readln(UserFile, Textstring);
  423.       // reading the questions , options and answers and asigning them to the right captions and labels in the form
  424.       lblQuestion.Caption := Textstring;
  425.       readln(UserFile, Textstring);
  426.       lblAnswer1.Caption := Textstring;
  427.       readln(UserFile, Textstring);
  428.       lblAnswer2.Caption := Textstring;
  429.       readln(UserFile, Textstring);
  430.       lblAnswer3.Caption := Textstring;
  431.       readln(UserFile, Textstring);
  432.       lblAnswer4.Caption := Textstring;
  433.       readln(UserFile, Textstring);
  434.       CorrAns := Textstring;
  435.       closefile(UserFile);
  436.       Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  437.       AnsClick := 'none';
  438.       Showmessage('Correct Answer');
  439.       lblAnswer1.Font.Color := ClBlack;
  440.       // the labels are black until they are clicked
  441.       lblAnswer2.Font.Color := ClBlack;
  442.       lblAnswer3.Font.Color := ClBlack;
  443.       lblAnswer4.Font.Color := ClBlack;
  444.     end
  445.     else
  446.     begin
  447.       closefile(UserFile);
  448.     end;
  449.   end
  450.   else if (AnsClick = 'none') then
  451.   begin
  452.     Showmessage('Please choose an answer');
  453.     // displaying error message if no answer is clicked before submission
  454.   end
  455.   else
  456.   begin
  457.     Showmessage('Incorrect Answer');
  458.     TimeW := TimeW + 1;
  459.     Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  460.     // keeping a counter of how many times an incorrect answer is given
  461.  
  462.   end;
  463.  
  464. end;
  465.  
  466. procedure TGeneralTopics.FormActivate(Sender: TObject);
  467. var
  468.   Ranques, RanNum, Redoing, i: integer;
  469.   RanNumSr, Tempo, Tempo2, scource, Quest: string;
  470.   ended, used: boolean;
  471. begin
  472.   QuesNum := 1;
  473.   TimeW := 0;
  474.   Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  475.   Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  476.   // keeping a counter of how many times an incorrect answer is given
  477.   AssignFile(UserFile, 'temp.txt');
  478.   Rewrite(UserFile);
  479.   closefile(UserFile);
  480.   ended := false;
  481.  
  482.   // for i := 1 to 10 do         //
  483.   // begin
  484.  
  485.   Redoing := 10;
  486.   while (Redoing > 0) do
  487.  
  488.   begin
  489.   //determining random coordinates for a random question to be picked and read out of either file A or B
  490.     ended := false;
  491.     used := true;
  492.     Ranques := Random(2);    //creating a random number either 0 or 1
  493.     Ranques := Ranques + 1;  //adding 1 for the random number to be either 1 or 2 to choose either file A or B
  494.  
  495.     RanNum := Random(10);   //Choosing integer of question number from that file to be randomly picked
  496.  
  497.     if Ranques = 1 then
  498.     begin
  499.       twoarray := 'A';   //in this case File A is picked
  500.     end;
  501.  
  502.     if Ranques = 2 then
  503.     begin
  504.       twoarray := 'B';   //in this case File B is picked
  505.     end;
  506.  
  507.     RanNumSr := inttostr(RanNum);
  508.  
  509.     if (RanNum < 10) then
  510.     begin
  511.       RanNumSr := '0' + RanNumSr;
  512.     end;
  513.  
  514.     twoarray := Concat(twoarray, RanNumSr);
  515.  
  516.     AssignFile(UserFile, 'temp.txt');
  517.     Reset(UserFile);
  518.     used := false;
  519.     while (* not EOF(UserFile) or ( *) ended = false do
  520.     begin
  521.  
  522.       readln(UserFile, Textstring);
  523.       if (twoarray = Textstring) then
  524.       begin
  525.         ended := true;
  526.         used := true;
  527.       end;
  528.       if (EOF(UserFile) and (used = false)) then
  529.       begin
  530.         ended := true; // tell program it is at the end of the file
  531.         used := false;
  532.         Redoing := Redoing - 1;
  533.       end;
  534.     end;
  535.     closefile(UserFile);
  536.     if used = false then
  537.     begin
  538.       AssignFile(SecondFile, 'temp.txt');
  539.       Append(SecondFile);
  540.       Writeln(SecondFile, twoarray);
  541.       closefile(SecondFile);
  542.       used := true;
  543.     end;
  544.  
  545.   end;
  546.  
  547.   // end;        //
  548.  
  549.   AssignFile(UserFile, 'temp.txt');
  550.   Reset(UserFile);
  551.   for i := 1 to 10 do
  552.   begin
  553.     readln(UserFile, Textstring);
  554.     q[i] := Textstring;
  555.   end;
  556.   closefile(UserFile);
  557.  
  558.   // Split String  //
  559.   for i := 1 to 10 do
  560.   begin
  561.     scource := q[i];
  562.     Tempo := copy(scource, 1, 1);
  563.     l[i] := Tempo;
  564.     Tempo := copy(scource, 2);
  565.     n[i] := Tempo;
  566.   end;
  567.   // String Split  //
  568.  
  569.   if (l[QuesNum] = 'A') then
  570.   begin
  571.     Quest := 'Questions.txt';
  572.   end
  573.   else
  574.   begin
  575.     Quest := 'Questions2.txt';
  576.   end;
  577.  
  578.   used := false;
  579.  
  580.   AssignFile(UserFile, Quest);
  581.   Reset(UserFile);
  582.   while (used = false) do
  583.   begin
  584.     readln(UserFile, Textstring);
  585.     if (Textstring = n[QuesNum]) then
  586.     begin
  587.       readln(UserFile, Textstring);
  588.       lblQuestion.Caption := Textstring;
  589.       readln(UserFile, Textstring);
  590.       lblAnswer1.Caption := Textstring;
  591.       readln(UserFile, Textstring);
  592.       lblAnswer2.Caption := Textstring;
  593.       readln(UserFile, Textstring);
  594.       lblAnswer3.Caption := Textstring;
  595.       readln(UserFile, Textstring);
  596.       lblAnswer4.Caption := Textstring;
  597.       readln(UserFile, Textstring);
  598.       CorrAns := Textstring; // determine current correct answer
  599.       used := true;
  600.     end
  601.     else
  602.       used := false;
  603.  
  604.   end;
  605.   closefile(UserFile);
  606. end;
  607.  
  608. procedure TGeneralTopics.lblAnswer1Click(Sender: TObject);
  609. begin
  610.   AnsClick := '1';
  611.   lblAnswer1.Font.Color := ClBlue;
  612.   lblAnswer2.Font.Color := ClBlack;
  613.   lblAnswer3.Font.Color := ClBlack;
  614.   lblAnswer4.Font.Color := ClBlack;
  615. end;
  616.  
  617. procedure TGeneralTopics.lblAnswer2Click(Sender: TObject);
  618. begin
  619.   AnsClick := '2';
  620.   lblAnswer1.Font.Color := ClBlack;
  621.   lblAnswer2.Font.Color := ClBlue;
  622.   lblAnswer3.Font.Color := ClBlack;
  623.   lblAnswer4.Font.Color := ClBlack;
  624. end;
  625.  
  626. procedure TGeneralTopics.lblAnswer3Click(Sender: TObject);
  627. begin
  628.   AnsClick := '3';
  629.   lblAnswer1.Font.Color := ClBlack;
  630.   lblAnswer2.Font.Color := ClBlack;
  631.   lblAnswer3.Font.Color := ClBlue;
  632.   lblAnswer4.Font.Color := ClBlack;
  633. end;
  634.  
  635. procedure TGeneralTopics.lblAnswer4Click(Sender: TObject);
  636. begin
  637.   AnsClick := '4';
  638.   lblAnswer1.Font.Color := ClBlack;
  639.   lblAnswer2.Font.Color := ClBlack;
  640.   lblAnswer3.Font.Color := ClBlack;
  641.   lblAnswer4.Font.Color := ClBlue;
  642. end;
  643.  
  644. end.
  645.  
  646. SPECIFIC TOPIC CHOICE UNIT
  647. unit UnitChoice;
  648.  
  649. interface
  650.  
  651. uses
  652.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  653.   System.Classes, Vcl.Graphics,
  654.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SpecificTopic1, SpecificTopic2,
  655.   Vcl.StdCtrls;
  656.  
  657. type
  658.   TFormTopicChoice = class(TForm)
  659.     Button1: TButton;
  660.     Button2: TButton;
  661.     procedure Button1Click(Sender: TObject);
  662.     procedure Button2Click(Sender: TObject);
  663.   private
  664.     { Private declarations }
  665.   public
  666.     { Public declarations }
  667.   end;
  668.  
  669. var
  670.   FormTopicChoice: TFormTopicChoice;
  671.  
  672. implementation
  673.  
  674. {$R *.dfm}
  675.  
  676. procedure TFormTopicChoice.Button1Click(Sender: TObject);
  677. begin
  678.   FormSpecificTopic1.show; // show specific quiz
  679. end;
  680.  
  681. procedure TFormTopicChoice.Button2Click(Sender: TObject);
  682. begin
  683.   FormSpecificTopic2.show; // show specific quiz
  684. end;
  685.  
  686. end.
  687.  
  688. SPECIFIC TOPIC 1 UNIT
  689.  
  690. unit SpecificTopic1;
  691.  
  692. interface
  693.  
  694. uses
  695.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  696.   System.Classes, Vcl.Graphics,
  697.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  698.  
  699. type
  700.   TFormSpecificTopic1 = class(TForm)
  701.     lblQuestion: TLabel;
  702.     lblAnswer1: TLabel;
  703.     lblAnswer2: TLabel;
  704.     lblAnswer3: TLabel;
  705.     lblAnswer4: TLabel;
  706.     btnSubmit: TButton;
  707.     Label1: TLabel;
  708.     Label2: TLabel;
  709.     procedure FormActivate(Sender: TObject);
  710.     procedure btnSubmitClick(Sender: TObject);
  711.     procedure FormCreate(Sender: TObject);
  712.     procedure lblAnswer1Click(Sender: TObject);
  713.     procedure lblAnswer2Click(Sender: TObject);
  714.     procedure lblAnswer3Click(Sender: TObject);
  715.     procedure lblAnswer4Click(Sender: TObject);
  716.   private
  717.     { Private declarations }
  718.   public
  719.     { Public declarations }
  720.     Userfile: textfile;
  721.     TextString, CorrAns, Ansclick: string;
  722.     TimeW: integer;
  723.   end;
  724.  
  725. var
  726.   FormSpecificTopic1: TFormSpecificTopic1;
  727.   QuesNum: integer;
  728.  
  729. implementation
  730.  
  731. {$R *.dfm}
  732.  
  733. procedure TFormSpecificTopic1.btnSubmitClick(Sender: TObject);
  734. Var
  735.   finding, enddd: boolean;
  736.   tempQN: string;
  737. begin
  738.   if (Ansclick = CorrAns) then
  739.   begin
  740.     AssignFile(Userfile, 'Questions.txt'); // Open the questions file
  741.     Reset(Userfile);
  742.     QuesNum := QuesNum + 1; // increment question number
  743.     tempQN := inttostr(QuesNum);
  744.     // designating the question number of the current question being asked
  745.     finding := true;
  746.     enddd := false;
  747.  
  748.     if (QuesNum < 10) then
  749.     begin
  750.       tempQN := '0' + tempQN;
  751.     end;
  752.  
  753.     while finding = true do
  754.     begin
  755.       readln(Userfile, TextString);
  756.       if (TextString = tempQN) then
  757.       // reading through the file till the next question to be asked is found (finding the right line in the file)
  758.       begin
  759.         finding := false;
  760.       end
  761.       else
  762.       begin
  763.         readln(Userfile, TextString); // reading the question
  764.         readln(Userfile, TextString); // reading option 1
  765.         readln(Userfile, TextString); // reading option 2
  766.         readln(Userfile, TextString); // reading option 3
  767.         readln(Userfile, TextString); // reading option 4
  768.         readln(Userfile, TextString); // reading correct answer
  769.       end;
  770.  
  771.       if EOF(Userfile) then // if its the the end of the file
  772.       begin
  773.         Showmessage('No More Questions');
  774.         // tell the user there are no more questions to be asked
  775.         finding := false;
  776.         enddd := true;
  777.       end;
  778.  
  779.     end;
  780.     if enddd = false then
  781.     begin
  782.       readln(Userfile, TextString);
  783.       // reading the questions , options and answers and asigning them to the right captions and labels in the form
  784.       lblQuestion.Caption := TextString;
  785.       readln(Userfile, TextString);
  786.       lblAnswer1.Caption := TextString;
  787.       readln(Userfile, TextString);
  788.       lblAnswer2.Caption := TextString;
  789.       readln(Userfile, TextString);
  790.       lblAnswer3.Caption := TextString;
  791.       readln(Userfile, TextString);
  792.       lblAnswer4.Caption := TextString;
  793.       readln(Userfile, TextString);
  794.       CorrAns := TextString;
  795.       closefile(Userfile);
  796.       Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  797.       Ansclick := 'none';
  798.       Showmessage('Correct Answer');
  799.       lblAnswer1.Font.Color := ClBlack;
  800.       // the labels are black until they are clicked
  801.       lblAnswer2.Font.Color := ClBlack;
  802.       lblAnswer3.Font.Color := ClBlack;
  803.       lblAnswer4.Font.Color := ClBlack;
  804.     end
  805.     else
  806.     begin
  807.       closefile(Userfile);
  808.     end;
  809.   end
  810.   else if (Ansclick = 'none') then
  811.   begin
  812.     Showmessage('Please choose an answer');
  813.     // displaying error message if no answer is clicked before submission
  814.   end
  815.   else
  816.   begin
  817.     Showmessage('Incorrect Answer');
  818.     TimeW := TimeW + 1;
  819.     Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  820.     // keeping a counter of how many times an incorrect answer is given
  821.  
  822.   end;
  823.  
  824. end;
  825.  
  826. procedure TFormSpecificTopic1.FormActivate(Sender: TObject);
  827. begin
  828.   AssignFile(Userfile, 'Questions.txt'); // open questions file
  829.   Reset(Userfile);
  830.   readln(Userfile, TextString);
  831.   QuesNum := strtoint(TextString);
  832.   Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  833.   // increment question number
  834.   readln(Userfile, TextString);
  835.   lblQuestion.Caption := TextString;
  836.   readln(Userfile, TextString);
  837.   lblAnswer1.Caption := TextString;
  838.   readln(Userfile, TextString);
  839.   lblAnswer2.Caption := TextString;
  840.   readln(Userfile, TextString);
  841.   lblAnswer3.Caption := TextString;
  842.   readln(Userfile, TextString);
  843.   lblAnswer4.Caption := TextString;
  844.   readln(Userfile, TextString);
  845.   CorrAns := TextString; // determine current correct answer
  846.   closefile(Userfile);
  847.   lblAnswer1.Font.Color := ClBlack;
  848.   lblAnswer2.Font.Color := ClBlack;
  849.   lblAnswer3.Font.Color := ClBlack;
  850.   lblAnswer4.Font.Color := ClBlack;
  851.  
  852. end;
  853.  
  854. procedure TFormSpecificTopic1.FormCreate(Sender: TObject);
  855. begin
  856.   Ansclick := 'none';
  857.   TimeW := 0;
  858.   Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  859. end;
  860.  
  861. procedure TFormSpecificTopic1.lblAnswer1Click(Sender: TObject);
  862. // changing each option to blue once it is clicked
  863. begin
  864.   Ansclick := '1';
  865.   lblAnswer1.Font.Color := ClBlue;
  866.   lblAnswer2.Font.Color := ClBlack;
  867.   lblAnswer3.Font.Color := ClBlack;
  868.   lblAnswer4.Font.Color := ClBlack;
  869. end;
  870.  
  871. procedure TFormSpecificTopic1.lblAnswer2Click(Sender: TObject);
  872. begin
  873.   Ansclick := '2';
  874.   lblAnswer1.Font.Color := ClBlack;
  875.   lblAnswer2.Font.Color := ClBlue;
  876.   lblAnswer3.Font.Color := ClBlack;
  877.   lblAnswer4.Font.Color := ClBlack;
  878. end;
  879.  
  880. procedure TFormSpecificTopic1.lblAnswer3Click(Sender: TObject);
  881. begin
  882.   Ansclick := '3';
  883.   lblAnswer1.Font.Color := ClBlack;
  884.   lblAnswer2.Font.Color := ClBlack;
  885.   lblAnswer3.Font.Color := ClBlue;
  886.   lblAnswer4.Font.Color := ClBlack;
  887. end;
  888.  
  889. procedure TFormSpecificTopic1.lblAnswer4Click(Sender: TObject);
  890. begin
  891.   Ansclick := '4';
  892.   lblAnswer1.Font.Color := ClBlack;
  893.   lblAnswer2.Font.Color := ClBlack;
  894.   lblAnswer3.Font.Color := ClBlack;
  895.   lblAnswer4.Font.Color := ClBlue;
  896. end;
  897.  
  898. end.
  899.  
  900. SPECIFIC TOPIC 2 UNIT
  901. unit SpecificTopic2;
  902.  
  903. interface
  904.  
  905. uses
  906.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  907.   System.Classes, Vcl.Graphics,
  908.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  909.  
  910. type
  911.   TFormSpecificTopic2 = class(TForm)
  912.     lblQuestion: TLabel;
  913.     lblAnswer1: TLabel;
  914.     lblAnswer2: TLabel;
  915.     lblAnswer3: TLabel;
  916.     lblAnswer4: TLabel;
  917.     Label1: TLabel;
  918.     Label2: TLabel;
  919.     btnSubmit: TButton;
  920.     procedure btnSubmitClick(Sender: TObject);
  921.     procedure FormActivate(Sender: TObject);
  922.     procedure FormCreate(Sender: TObject);
  923.     procedure lblAnswer1Click(Sender: TObject);
  924.     procedure lblAnswer2Click(Sender: TObject);
  925.     procedure lblAnswer3Click(Sender: TObject);
  926.     procedure lblAnswer4Click(Sender: TObject);
  927.   private
  928.     { Private declarations }
  929.   public
  930.     { Public declarations }
  931.     Userfile: textfile;
  932.     TextString, CorrAns, Ansclick: string;
  933.     TimeW: integer;
  934.   end;
  935.  
  936. var
  937.   FormSpecificTopic2: TFormSpecificTopic2;
  938.   QuesNum: integer;
  939.  
  940. implementation
  941.  
  942. {$R *.dfm}
  943.  
  944. procedure TFormSpecificTopic2.btnSubmitClick(Sender: TObject);
  945. Var
  946.   finding, enddd: boolean;
  947.   tempQN: string;
  948. begin
  949.   if (Ansclick = CorrAns) then
  950.   begin
  951.     AssignFile(Userfile, 'Questions2.txt'); // Open the questions file
  952.     Reset(Userfile);
  953.     QuesNum := QuesNum + 1; // increment question number
  954.     tempQN := inttostr(QuesNum);
  955.     // designating the question number of the current question being asked
  956.     finding := true;
  957.     enddd := false;
  958.  
  959.     if (QuesNum < 10) then
  960.     begin
  961.       tempQN := '0' + tempQN;
  962.     end;
  963.  
  964.     while finding = true do
  965.     begin
  966.       readln(Userfile, TextString);
  967.       if (TextString = tempQN) then
  968.       // reading through the file till the next question to be asked is found (finding the right line in the file)
  969.       begin
  970.         finding := false;
  971.       end
  972.       else
  973.       begin
  974.         readln(Userfile, TextString); // reading the question
  975.         readln(Userfile, TextString); // reading option 1
  976.         readln(Userfile, TextString); // reading option 2
  977.         readln(Userfile, TextString); // reading option 3
  978.         readln(Userfile, TextString); // reading option 4
  979.         readln(Userfile, TextString); // reading correct answer
  980.       end;
  981.  
  982.       if EOF(Userfile) then // if it’s the end of the file
  983.       begin
  984.         Showmessage('No More Questions');
  985.         // tell the user there are no more questions to be asked
  986.         finding := false;
  987.         enddd := true;
  988.       end;
  989.  
  990.     end;
  991.     if enddd = false then
  992.     begin
  993.       readln(Userfile, TextString);
  994.       // reading the questions , options and answers and asigning them to the right captions and labels in the form
  995.       lblQuestion.Caption := TextString;
  996.       readln(Userfile, TextString);
  997.       lblAnswer1.Caption := TextString;
  998.       readln(Userfile, TextString);
  999.       lblAnswer2.Caption := TextString;
  1000.       readln(Userfile, TextString);
  1001.       lblAnswer3.Caption := TextString;
  1002.       readln(Userfile, TextString);
  1003.       lblAnswer4.Caption := TextString;
  1004.       readln(Userfile, TextString);
  1005.       CorrAns := TextString;
  1006.       closefile(Userfile);
  1007.       Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  1008.       Ansclick := 'none';
  1009.       Showmessage('Correct Answer');
  1010.       lblAnswer1.Font.Color := ClBlack;
  1011.       // the labels are black until they are clicked
  1012.       lblAnswer2.Font.Color := ClBlack;
  1013.       lblAnswer3.Font.Color := ClBlack;
  1014.       lblAnswer4.Font.Color := ClBlack;
  1015.     end
  1016.     else
  1017.     begin
  1018.       closefile(Userfile);
  1019.     end;
  1020.   end
  1021.   else if (Ansclick = 'none') then
  1022.   begin
  1023.     Showmessage('Please choose an answer');
  1024.     // displaying error message if no answer is clicked before submission
  1025.   end
  1026.   else
  1027.   begin
  1028.     Showmessage('Incorrect Answer');
  1029.     TimeW := TimeW + 1;
  1030.     Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  1031.     // keeping a counter of how many times an incorrect answer is given
  1032.  
  1033.   end;
  1034.  
  1035. end;
  1036.  
  1037. procedure TFormSpecificTopic2.FormActivate(Sender: TObject);
  1038. begin
  1039.   AssignFile(Userfile, 'Questions2.txt'); // open questions file
  1040.   Reset(Userfile);
  1041.   readln(Userfile, TextString);
  1042.   QuesNum := strtoint(TextString);
  1043.   Label1.Caption := ('Question Number ' + inttostr(QuesNum));
  1044.   // increment question number
  1045.   readln(Userfile, TextString);
  1046.   lblQuestion.Caption := TextString;
  1047.   readln(Userfile, TextString);
  1048.   lblAnswer1.Caption := TextString;
  1049.   readln(Userfile, TextString);
  1050.   lblAnswer2.Caption := TextString;
  1051.   readln(Userfile, TextString);
  1052.   lblAnswer3.Caption := TextString;
  1053.   readln(Userfile, TextString);
  1054.   lblAnswer4.Caption := TextString;
  1055.   readln(Userfile, TextString);
  1056.   CorrAns := TextString; // determine current correct answer
  1057.   closefile(Userfile);
  1058.   lblAnswer1.Font.Color := ClBlack;
  1059.   lblAnswer2.Font.Color := ClBlack;
  1060.   lblAnswer3.Font.Color := ClBlack;
  1061.   lblAnswer4.Font.Color := ClBlack;
  1062.  
  1063. end;
  1064.  
  1065. procedure TFormSpecificTopic2.FormCreate(Sender: TObject);
  1066. begin
  1067.   Ansclick := 'none';
  1068.   TimeW := 0;
  1069.   Label2.Caption := ('Number Incorrect: ' + inttostr(TimeW));
  1070. end;
  1071.  
  1072. procedure TFormSpecificTopic2.lblAnswer1Click(Sender: TObject);
  1073. begin
  1074.   Ansclick := '1';
  1075.   lblAnswer1.Font.Color := ClBlue;
  1076.   lblAnswer2.Font.Color := ClBlack;
  1077.   lblAnswer3.Font.Color := ClBlack;
  1078.   lblAnswer4.Font.Color := ClBlack;
  1079. end;
  1080.  
  1081. procedure TFormSpecificTopic2.lblAnswer2Click(Sender: TObject);
  1082. begin
  1083.   Ansclick := '2';
  1084.   lblAnswer1.Font.Color := ClBlack;
  1085.   lblAnswer2.Font.Color := ClBlue;
  1086.   lblAnswer3.Font.Color := ClBlack;
  1087.   lblAnswer4.Font.Color := ClBlack;
  1088. end;
  1089.  
  1090. procedure TFormSpecificTopic2.lblAnswer3Click(Sender: TObject);
  1091. begin
  1092.   Ansclick := '3';
  1093.   lblAnswer1.Font.Color := ClBlack;
  1094.   lblAnswer2.Font.Color := ClBlack;
  1095.   lblAnswer3.Font.Color := ClBlue;
  1096.   lblAnswer4.Font.Color := ClBlack;
  1097. end;
  1098.  
  1099. procedure TFormSpecificTopic2.lblAnswer4Click(Sender: TObject);
  1100. begin
  1101.   Ansclick := '4';
  1102.   lblAnswer1.Font.Color := ClBlack;
  1103.   lblAnswer2.Font.Color := ClBlack;
  1104.   lblAnswer3.Font.Color := ClBlack;
  1105.   lblAnswer4.Font.Color := ClBlue;
  1106. end;
  1107.  
  1108. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement