Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.14 KB | None | 0 0
  1. unit Unit_DeleteUser;
  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;
  8.  
  9. type
  10.   TfrmDeleteUser = class(TForm)
  11.     lbledtAccount: TLabeledEdit;
  12.     MemoAccount: TMemo;
  13.     btnEnter: TButton;
  14.     btnExit: TButton;
  15.     btnDelete: TButton;
  16.     CoBoxAccount: TComboBox;
  17.     procedure btnExitClick(Sender: TObject);
  18.     procedure btnEnterClick(Sender: TObject);
  19.     procedure CoBoxAccountChange(Sender: TObject);
  20.     procedure btnDeleteClick(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   frmDeleteUser: TfrmDeleteUser;
  29.  
  30. implementation
  31.  uses
  32.  Unit_Login;
  33. {$R *.dfm}
  34. var
  35. TeacherID, StudentID : Boolean;
  36. Count,StudentMatch: Integer;  //Variables used for students
  37. StudentExists,TeacherExists: Boolean;
  38. StudentUser, StudentPassword : TStrings;
  39. TeacherUser, TeacherPassword : Tstrings;
  40. TeacherMatch : Integer;
  41. procedure TfrmDeleteUser.btnDeleteClick(Sender: TObject);
  42. Var
  43. N,Counter: Integer;
  44. begin
  45.  if CoboxAccount.Text = 'Student Account' then
  46.   begin
  47.    //Uses the variables from the login form
  48.    StudentUser.Delete(StudentMatch);  //String list for StudentNumbers
  49.    StudentPassword.Delete(StudentMatch);//String list for Student's passwords
  50.    AssignFile(frmLogin.StudentNumberFile, 'StudentNumber.Dat');
  51.    ReWrite(frmLogin.StudentNumberFile);
  52.    AssignFile(frmLogin.PasswordFile, 'Password.Dat');
  53.    ReWrite(frmLogin.PasswordFile);
  54.    {The count is reduced to ensure the stringlists do not go out of bounds}
  55.     for N := 0 to Count - 2 do
  56.      begin
  57.         //The following two lines copy the cotent of the stringlist into the main array
  58.         frmLogin.Student_Number[N].StudentNumber := StudentUser[N];
  59.         frmLogin.Student_Password[N].StudentPassword := StudentPassword[N];
  60.         //This writes back all the accounts into the array which now does not include the deleted one
  61.         Write(frmLogin.StudentNumberFile,frmLogin.Student_Number[N]);
  62.         Write(frmLogin.PasswordFile,frmLogin.Student_Password[N]);
  63.      end;
  64.     CloseFile(frmLogin.StudentNumberFile);
  65.     CloseFile(frmLogin.PasswordFile);
  66.     Showmessage('Student account has been deleted');
  67.   end;
  68.  
  69. if CoboxAccount.Text = 'Teacher Account' then
  70.  begin
  71.    TeacherUser.Delete(TeacherMatch);
  72.    TeacherPassword.Delete(TeacherMatch);
  73.    AssignFile(frmLogin.TeacherUsernameFile, 'TeacherUser.Dat');
  74.    ReWrite(frmLogin.TeacherUsernameFile);
  75.    AssignFile(frmLogin.TeacherPasswordFile, 'TeacherPassword.Dat');
  76.    ReWrite(frmLogin.TeacherPasswordFile);
  77.    for N := 0 to Count - 2 do
  78.     begin
  79.      frmLogin.Teacher_Username[N].TeacherUsername := TeacherUser[N];
  80.      frmLogin.Teacher_Password[N].TeacherPassword := TeacherPassword[N];
  81.      Write(frmLogin.TeacherUsernameFile, frmLogin.Teacher_Username[N]);
  82.      Write(frmLogin.TeacherPasswordFile, frmLogin.Teacher_Password[N]);
  83.     end;
  84.     Showmessage('Teacher account has been deleted');
  85.  end;
  86. end;
  87.  
  88. procedure TfrmDeleteUser.btnEnterClick(Sender: TObject);
  89. begin
  90. if CoboxAccount.Text = 'Student Account' then
  91.    begin
  92.     //Uses the public variables from the login form
  93.     AssignFile(frmLogin.StudentNumberFile, 'StudentNumber.Dat');
  94.     Reset(frmLogin.StudentNumberFile);
  95.     AssignFile(frmLogin.PasswordFile, 'Password.Dat');
  96.     Reset(frmLogin.PasswordFile);
  97.     Count := 0;
  98.     {StudentUser and StudentPassword are Stringlist}
  99.     StudentUser := TStringList.Create;
  100.     StudentPassword := TStringList.Create;
  101.     StudentExists := False;
  102.     //Reads all the studentnumber and passwords in the file
  103.     while not eof (frmLogin.StudentNumberFile) and not eof (frmLogin.PasswordFile) do
  104.      begin
  105.        Read(frmLogin.StudentNumberFile,frmLogin.Student_Number[Count]);
  106.        Read(frmLogin.PasswordFile,frmLogin.Student_Password[Count]);
  107.        //The contents of the array are coppied into both stringlists
  108.        StudentUser.Add(frmLogin.Student_Number[Count].StudentNumber);
  109.        StudentPassword.Add(frmLogin.Student_Password[Count].StudentPassword);
  110.        //The if statement checks if the student numbers matches any in the array
  111.        if frmLogin.Student_Number[Count].StudentNumber = lbledtAccount.Text then
  112.         begin
  113.           StudentMatch := Count;
  114.           //Clears the memo of any text
  115.           MemoAccount.Clear;
  116.           //Adds the account credentials of the account searched for
  117.           MemoAccount.Lines.Add('Student Number : '+ frmLogin.Student_Number[Count].StudentNumber +
  118.           ' Password : '+frmLogin.Student_Password[Count].StudentPassword);
  119.           StudentExists := True;
  120.         end;
  121.         Count := Count + 1;
  122.      end;
  123.     CloseFile(frmLogin.StudentNumberFile);
  124.     CloseFile(FrmLogin.PasswordFile);
  125.      if StudentExists = False then
  126.      begin
  127.        Showmessage('Student Account does not exists');
  128.      end;
  129.    end;
  130.  if CoboxAccount.Text = 'Teacher Account' then
  131.   begin
  132.     AssignFile(frmLogin.TeacherUsernameFile, 'TeacherUser.Dat');
  133.     Reset(frmLogin.TeacherUsernameFile);
  134.     AssignFile(frmLogin.TeacherPasswordFile, 'TeacherPassword.Dat');
  135.     Reset(frmLogin.TeacherPasswordFile);
  136.     Count := 0;
  137.     TeacherUser := TStringList.Create;
  138.     TeacherPassword := TStringList.Create;
  139.     TeacherExists := False;
  140.     while not eof (frmLogin.TeacherUsernameFile) and not eof (frmLogin.TeacherPasswordFile) do
  141.      begin
  142.        Read(frmLogin.TeacherUsernameFile,frmLogin.Teacher_Username[Count]);
  143.        Read(frmLogin.TeacherPasswordFile,frmLogin.Teacher_Password[Count]);
  144.        TeacherUser.Add(frmLogin.Teacher_Username[Count].TeacherUsername);
  145.        TeacherPassword.Add(frmLogin.Teacher_Password[Count].TeacherPassword);
  146.        if frmLogin.Teacher_Username[Count].TeacherUsername = lbledtAccount.Text  then
  147.         begin
  148.           TeacherMatch := Count;
  149.           MemoAccount.Clear;
  150.           MemoAccount.Lines.Add('Username : '+frmLogin.Teacher_Username[Count].TeacherUsername
  151.           +' Password: ' + frmLogin.Teacher_Password[Count].TeacherPassword);
  152.           TeacherExists := True;
  153.         end;
  154.         Count := Count + 1;
  155.      end;
  156.       if TeacherExists = False then
  157.        begin
  158.          Showmessage('This Teacher account does not exist');
  159.        end;
  160.   end;
  161.  
  162.  
  163.  
  164. end;
  165.  
  166. procedure TfrmDeleteUser.btnExitClick(Sender: TObject);
  167. begin
  168.   Close;
  169. end;
  170.  
  171. procedure TfrmDeleteUser.CoBoxAccountChange(Sender: TObject);
  172. begin
  173.   //If the User selects the teader acount in the combobox the edit label will change to teacher ID
  174.   if CoboxAccount.Text = 'Teacher Account' then
  175.    begin
  176.     lbledtAccount.EditLabel.Caption := 'Teacher ID';
  177.     //The two following boolean variables tells the system which acconts needs to be deleted.
  178.     TeacherID := True;
  179.     StudentID := False;
  180.    end;
  181.   //If the User selects the Student acount in the combobox the edit label will change to student ID
  182.   if CoboxAccount.Text = 'Student Account'  then
  183.    begin
  184.      //The two following boolean variables tells the system which acconts needs to be deleted.
  185.      lbledtAccount.EditLabel.Caption := 'Student ID';
  186.      TeacherID := False;
  187.      StudentID := True;
  188.    end;
  189. end;
  190.  
  191. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement