Advertisement
UHaroon

Untitled

Apr 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.47 KB | None | 0 0
  1. unit UnitAddHomework;
  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, MmSystem;
  8.  
  9. type
  10.   TfrmAddHomework = class(TForm)
  11.     lblAddHomework: TLabel;
  12.     ledtHomeworkName: TLabeledEdit;
  13.     btnAddHomework: TButton;
  14.     btnClose: TButton;
  15.     cboAHSelectGroup: TComboBox;
  16.     Label1: TLabel;
  17.     procedure btnCloseClick(Sender: TObject);
  18.     procedure btnAddHomeworkClick(Sender: TObject);
  19.     procedure ledtHomeworkNameKeyPress(Sender: TObject; var Key: Char);
  20.     procedure FormShow(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   frmAddHomework: TfrmAddHomework;
  29.  
  30. implementation
  31.  
  32. uses UnitGroupProfiles, UnitdmDatabasecomponents;
  33.  
  34. {$R *.dfm}
  35.  
  36. procedure ErrorOutput(ErrorMessage : string; Ledt : TLabeledEdit);
  37. begin
  38.   {  ----------  Highlight the input with an error  ----------  }
  39.   Ledt.Color := clRed;  // Turn the input into a red color
  40.   ledt.Font.Color := clWhite;  // Turn the text into the input to white
  41.   PlaySound('SYSTEMEXCLAMATION', 0, SND_ASYNC);  //  Play error sound
  42.   Showmessage(ErrorMessage);  //  Output an error message
  43.   {  ----------  Remove the highlight  ----------}
  44.   Ledt.Color := clWhite;  // Make the input white again
  45.   ledt.Font.Color := clBlack;  //  Make the font black again
  46. end;
  47.  
  48. procedure TfrmAddHomework.btnAddHomeworkClick(Sender: TObject);
  49. var
  50.   ErrorMessagePram : string;
  51. begin
  52.   { ----------  Validation  ---------- }
  53.   { ---------- Assessment Name ---------- }
  54.   { ----------  Length Check  ---------- }
  55.   if Length(ledtHomeworkName.Text) > 50 then  //  Check to see if input length is greater than 50
  56.   begin
  57.     ErrorMessagePram := '';
  58.     ErrorMessagePram := 'ERROR: Homework name must be 50 characters or less';
  59.     ErrorOutput(ErrorMessagePram, ledtHomeworkName);
  60.   end    //  End of else if statement
  61.  
  62.   { ----------  Presence Check  ---------- }
  63.   else if ledtHomeworkName.Text = '' then  //  if the assessment title is empty
  64.   begin
  65.     ErrorMessagePram := '';
  66.     ErrorMessagePram := 'ERROR: Please enter a homework name';
  67.     ErrorOutput(ErrorMessagePram, ledtHomeworkName);
  68.   end  // end else if
  69.  
  70.   { ----------  Group Name  ---------- }
  71.  
  72.   {  ----------  Presence Check  ----------  }
  73.   else if cboAHSelectGroup.Text = '' then  // Check to see if input is empty
  74.   begin
  75.     ErrorMessagePram := '';
  76.     ErrorMessagePram := 'ERROR: Please enter a Group Name';
  77.     ErrorOutput(ErrorMessagePram, ledtAHGroupName);
  78.   end    //  End of else if statement
  79.  
  80.   else
  81.   begin
  82.     { ----------  Add a column as an Homework  ---------- }
  83.     with dmDatabaseComponents.adoUnitAddHomework do
  84.     begin
  85.       SQL.Clear; // Initialize the SQL
  86.       // Set SQL
  87.       SQL.Text:='ALTER TABLE [HOMEWORK'+cboAHSelectGroup.Items[cboAHSelectGroup.ItemIndex]+'] ADD ['+ledtHomeworkName.Text+'] varchar(50)';
  88.       ExecSQL;  // Execute SQL
  89.     end;  //  End of with statement
  90.  
  91.    Showmessage('Homework has successfully been added');  // Produce a confirmation message
  92.    {  ----------    ----------  }
  93.    // Once homework is saved clear inputs
  94.    ledtHomeworkName.Text := '';
  95.    cboAHSelectGroup.Text := '';
  96.   end;
  97. end;
  98.  
  99. procedure TfrmAddHomework.btnCloseClick(Sender: TObject);
  100. begin
  101.   close;
  102. end;
  103.  
  104. procedure TfrmAddHomework.FormShow(Sender: TObject);
  105. begin
  106.   with dmDatabaseComponents.adoASDisplayGroup do
  107.   begin
  108.     Close;
  109.     SQL.Clear;  //  Initialize the SQL
  110.     //  Set the SQL
  111.     SQL.Text := 'SELECT GroupName FROM GroupDetails';
  112.     ExecSQL;  //  Execute the SQL
  113.     Open;
  114.  
  115.     cboAHSelectGroup.Clear; // Clear all contents of the combo box
  116.     First;  //  Start at the first record
  117.     while not Eof do
  118.     begin
  119.       //  Add the group name to the current contents of the combo box
  120.       cboAHSelectGroup.Items.Add(FieldByName('GroupName').AsString);
  121.       Next;  //  Move to the next record
  122.     end;  //  End of while loop
  123.   end;  //  end of with statement
  124.  
  125. end;
  126.  
  127. procedure TfrmAddHomework.ledtHomeworkNameKeyPress(Sender: TObject;
  128.   var Key: Char);
  129. begin
  130.   if (not (Key in ['A'..'Z','a'..'z','-','/','0'..'9', #32])) then // If a letter key is not pressed
  131.   begin
  132.     PlaySound('SYSTEMEXCLAMATION', 0, SND_ASYNC);  //  Play error sound
  133.     Showmessage('ERROR: Invalid key entered for group name');  //  Produce an error message
  134.     Key := #0; // Remove the last character
  135.   end; // end if
  136. end;
  137.  
  138. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement