Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 30.56 KB | None | 0 0
  1. unit InputOfRecords;
  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.Grids, Vcl.Menus,
  8.   Vcl.Imaging.pngimage, Vcl.ExtCtrls;
  9.  
  10. type
  11.   TInputRec = class(TForm)
  12.     EdSurname: TEdit;
  13.     LblSurname: TLabel;
  14.     EdName: TEdit;
  15.     EdPatronymic: TEdit;
  16.     LblName: TLabel;
  17.     LblPatronymic: TLabel;
  18.     LblGroup: TLabel;
  19.     EdNumber: TEdit;
  20.     LblNumber: TLabel;
  21.     LblDateOfBirth: TLabel;
  22.     LblMonth: TLabel;
  23.     EdMonth: TEdit;
  24.     LblDay: TLabel;
  25.     EdDay: TEdit;
  26.     LblYear: TLabel;
  27.     EdYear: TEdit;
  28.     Table: TStringGrid;
  29.     BtCreate: TButton;
  30.     BtReady: TButton;
  31.     PopupMenu1: TPopupMenu;
  32.     RadioButtonMale: TRadioButton;
  33.     RadioButtonFemale: TRadioButton;
  34.     ImageHelp: TImage;
  35.     ComboBoxGroup: TComboBox;
  36.     BtChange: TButton;
  37.     OpenDialog: TOpenDialog;
  38.     InputFromFile: TButton;
  39.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  40.     procedure BtReadyClick(Sender: TObject);
  41.     procedure FormCreate(Sender: TObject);
  42.     procedure EdSurnameKeyPress(Sender: TObject; var Key: Char);
  43.     procedure EdNameKeyPress(Sender: TObject; var Key: Char);
  44.     procedure EdPatronymicKeyPress(Sender: TObject; var Key: Char);
  45.     procedure EdNumberKeyPress(Sender: TObject; var Key: Char);
  46.     procedure ImageHelpClick(Sender: TObject);
  47.     procedure EdYearKeyPress(Sender: TObject; var Key: Char);
  48.     procedure EdMonthKeyPress(Sender: TObject; var Key: Char);
  49.     procedure EdDayKeyPress(Sender: TObject; var Key: Char);
  50.     procedure EdSurnameChange(Sender: TObject);
  51.     procedure EdNameChange(Sender: TObject);
  52.     procedure EdPatronymicChange(Sender: TObject);
  53.     procedure EdNumberChange(Sender: TObject);
  54.     procedure EdYearChange(Sender: TObject);
  55.     procedure EdMonthChange(Sender: TObject);
  56.     procedure EdDayChange(Sender: TObject);
  57.     procedure RadioButtonMaleClick(Sender: TObject);
  58.     procedure RadioButtonFemaleClick(Sender: TObject);
  59.     procedure ComboBoxGroupKeyPress(Sender: TObject; var Key: Char);
  60.     procedure ComboBoxGroupChange(Sender: TObject);
  61.     procedure BtCreateClick(Sender: TObject);
  62.     procedure RadioButtonMaleKeyPress(Sender: TObject; var Key: Char);
  63.     procedure RadioButtonFemaleKeyPress(Sender: TObject; var Key: Char);
  64.     procedure TableMouseDown(Sender: TObject; Button: TMouseButton;
  65.       Shift: TShiftState; X, Y: Integer);
  66.     procedure BtChangeClick(Sender: TObject);
  67.     procedure InputFromFileClick(Sender: TObject);
  68.   private
  69.     { Private declarations }
  70.   public
  71.     { Public declarations }
  72.   end;
  73.  
  74. var
  75.   InputRec: TInputRec;
  76.  
  77. implementation
  78.  
  79. {$R *.dfm}
  80.  
  81. uses MainForm, ViewRecord;
  82.  
  83. var
  84.    ChangeRow: Integer;
  85.  
  86. procedure ChangeBuf(const Table: TStringGrid; var Bufer: MainForm.TArr);
  87. begin
  88.    SetLength(MainForm.Bufer, Length(MainForm.Bufer) + 1);
  89.    MainForm.Bufer[High(MainForm.Bufer)].Surname := Table.Cells[0, Table.RowCount-1];
  90.    MainForm.Bufer[High(MainForm.Bufer)].Name := Table.Cells[1, Table.RowCount-1];
  91.    MainForm.Bufer[High(MainForm.Bufer)].Patronymic := Table.Cells[2, Table.RowCount-1];
  92.    MainForm.Bufer[High(MainForm.Bufer)].Group := StrToInt(Table.Cells[3, Table.RowCount-1]);
  93.    MainForm.Bufer[High(MainForm.Bufer)].Number := StrToInt(Table.Cells[4, Table.RowCount-1]);
  94.    MainForm.Bufer[High(MainForm.Bufer)].DateOfBirth := Table.Cells[5, Table.RowCount-1];
  95.    MainForm.Bufer[High(MainForm.Bufer)].Sex := Table.Cells[6, Table.RowCount-1];
  96. end;
  97.  
  98. procedure TInputRec.BtChangeClick(Sender: TObject);
  99. var
  100.    Mon, D, Num: string;
  101.    i: Integer;
  102. begin
  103.    BtReady.Enabled := False;
  104.    EdSurname.Text := Table.Cells[0, ChangeRow];
  105.    EdName.Text := Table.Cells[1, ChangeRow];
  106.    EdPatronymic.Text := Table.Cells[2, ChangeRow];
  107.    i := 0;
  108.    while ComboBoxGroup.Items[i] <> Table.Cells[3, ChangeRow] do
  109.    begin
  110.       Inc(i);
  111.    end;
  112.    ComboBoxGroup.ItemIndex := i;
  113.    Table.Cells[3, ChangeRow] := '';
  114.    Num := Table.Cells[4, ChangeRow];
  115.    Table.Cells[4, ChangeRow] := '';
  116.    EdNumber.Text := Num;
  117.    EdYear.Text := Copy(Table.Cells[5, ChangeRow], Length(Table.Cells[5, ChangeRow]) - 3, 4);
  118.    D := Table.Cells[5, ChangeRow][1];
  119.    if Table.Cells[5, ChangeRow][2] <> '.' then
  120.    begin
  121.       D := D + Table.Cells[5, ChangeRow][2];
  122.       Mon := Table.Cells[5, ChangeRow][4];
  123.       if Table.Cells[5, ChangeRow][5] <> '.' then
  124.          Mon := Mon + Table.Cells[5, ChangeRow][5];
  125.    end
  126.    else
  127.    begin
  128.       Mon := Table.Cells[5, ChangeRow][3];
  129.       if Table.Cells[5, ChangeRow][4] <> '.' then
  130.          Mon := Mon + Table.Cells[5, ChangeRow][4];
  131.    end;
  132.    EdMonth.Text := Mon;
  133.    EdDay.Text := D;
  134.    if Table.Cells[6, ChangeRow] = 'male' then
  135.       RadioButtonMale.Checked := True
  136.    else
  137.       RadioButtonFemale.Checked := True;
  138.    Table.Rows[ChangeRow].Clear;
  139.    BtCreate.Caption := 'Apply';
  140.    BtChange.Enabled := False;
  141. end;
  142.  
  143. procedure TInputRec.BtCreateClick(Sender: TObject);
  144. begin
  145.    if BtCreate.Caption = 'Create' then
  146.    begin
  147.       Table.RowCount := Table.RowCount + 1;
  148.       Table.Cells[0, Table.RowCount-1] := EdSurname.Text;
  149.       Table.Cells[1, Table.RowCount-1] := EdName.Text;
  150.       Table.Cells[2, Table.RowCount-1] := EdPatronymic.Text;
  151.       Table.Cells[3, Table.RowCount-1] := ComboBoxGroup.Text;
  152.       Table.Cells[4, Table.RowCount-1] := EdNumber.Text;
  153.       Table.Cells[5, Table.RowCount-1] := EdDay.Text + '.' + EdMonth.Text + '.' + EdYear.Text;
  154.       if RadioButtonMale.Checked then
  155.          Table.Cells[6, Table.RowCount-1] := RadioButtonMale.Caption
  156.       else
  157.          Table.Cells[6, Table.RowCount-1] := RadioButtonFemale.Caption;
  158.       RadioButtonMale.Checked := false;
  159.       RadioButtonFemale.Checked := false;
  160.       EdSurname.Clear;
  161.       EdName.Clear;
  162.       EdPatronymic.Clear;
  163.       EdNumber.Clear;
  164.       EdYear.Clear;
  165.       EdMonth.Clear;
  166.       EdDay.Clear;
  167.       ComboBoxGroup.ItemIndex := -1;
  168.       ChangeBuf(Table, MainForm.Bufer);
  169.       EdSurname.SetFocus;
  170.    end
  171.    else
  172.    begin
  173.       Table.Cells[0, ChangeRow] := EdSurname.Text;
  174.       Table.Cells[1, ChangeRow] := EdName.Text;
  175.       Table.Cells[2, ChangeRow] := EdPatronymic.Text;
  176.       Table.Cells[3, ChangeRow] := ComboBoxGroup.Text;
  177.       Table.Cells[4, ChangeRow] := EdNumber.Text;
  178.       Table.Cells[5, ChangeRow] := EdDay.Text + '.' + EdMonth.Text + '.' + EdYear.Text;
  179.       if RadioButtonMale.Checked then
  180.          Table.Cells[6, ChangeRow] := RadioButtonMale.Caption
  181.       else
  182.          Table.Cells[6, ChangeRow] := RadioButtonFemale.Caption;
  183.       MainForm.Bufer[ChangeRow - 1].Surname := Table.Cells[0, ChangeRow];
  184.       MainForm.Bufer[ChangeRow - 1].Name := Table.Cells[1, ChangeRow];
  185.       MainForm.Bufer[ChangeRow - 1].Patronymic := Table.Cells[2, ChangeRow];
  186.       MainForm.Bufer[ChangeRow - 1].Group := StrToInt(Table.Cells[3, ChangeRow]);
  187.       MainForm.Bufer[ChangeRow - 1].Number := StrToInt(Table.Cells[4, ChangeRow]);
  188.       MainForm.Bufer[ChangeRow - 1].DateOfBirth := Table.Cells[5, ChangeRow];
  189.       MainForm.Bufer[ChangeRow - 1].Sex := Table.Cells[6, ChangeRow];
  190.       RadioButtonMale.Checked := false;
  191.       RadioButtonFemale.Checked := false;
  192.       EdSurname.Clear;
  193.       EdName.Clear;
  194.       EdPatronymic.Clear;
  195.       EdNumber.Clear;
  196.       EdYear.Clear;
  197.       EdMonth.Clear;
  198.       EdDay.Clear;
  199.       ComboBoxGroup.ItemIndex := -1;
  200.       ChangeBuf(Table, MainForm.Bufer);
  201.       BtCreate.Caption := 'Create';
  202.       BtChange.Enabled := False;
  203.       ViewRec.BtSort.Enabled := True;
  204.       BtReady.Enabled := True;
  205.    end;
  206. end;
  207.  
  208. procedure TInputRec.BtReadyClick(Sender: TObject);
  209. begin
  210.    if Table.RowCount > 1 then
  211.    begin
  212.       BtReady.Enabled := false;
  213.       Close;
  214.    end
  215.    else
  216.       if (Application.MessageBox('You haven''t created the records. Would you like to close the window?',
  217.       'Window closing', MB_OKCANCEL) = mrOk) then
  218.       begin
  219.          BtReady.Enabled := false;
  220.          Close;
  221.       end;
  222.  
  223. end;
  224.  
  225. function CheckNumber(val: Integer; grp: string): Boolean;
  226. var
  227.    i: Integer;
  228.    IsCor: Boolean;
  229. begin
  230.    IsCor := True;
  231.    i := 1;
  232.    repeat
  233.       if grp = InputRec.Table.Cells[3,i] then
  234.          if val = StrToInt(InputRec.Table.Cells[4,i]) then
  235.             IsCor := False
  236.          else
  237.             Inc(i)
  238.       else
  239.          Inc(i);
  240.    until (i >= InputRec.Table.RowCount) or not IsCor;
  241.    CheckNumber := IsCor;
  242. end;
  243.  
  244.  
  245. procedure TInputRec.ComboBoxGroupChange(Sender: TObject);
  246. begin
  247.    EdNumber.Enabled := True;
  248.    if Length(EdNumber.Text) > 0 then
  249.       if CheckNumber(StrToInt(EdNumber.Text), ComboBoxGroup.Text) then
  250.       begin
  251.          EdNumber.Color := $00CDFF9B;
  252.          if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) then
  253.             BtCreate.Enabled := True;
  254.       end
  255.       else
  256.       begin
  257.          EdNumber.Color := $00BBBBFF;
  258.          BtCreate.Enabled := False;
  259.       end;
  260. end;
  261.  
  262. procedure TInputRec.ComboBoxGroupKeyPress(Sender: TObject; var Key: Char);
  263. begin
  264.    if Key = #13 then
  265.       if EdNumber.Enabled then
  266.       begin
  267.          Key := #0;
  268.          EdNumber.SetFocus;
  269.       end
  270.       else
  271.       begin
  272.          Key := #0;
  273.          EdYear.SetFocus;
  274.       end
  275. end;
  276.  
  277. procedure TInputRec.EdDayChange(Sender: TObject);
  278. type
  279.    TArr = array[1..12] of Integer;
  280. const
  281.    NumOfDays: TArr = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  282. var
  283.    Year, Num: Integer;
  284. begin
  285.    if Length(EdDay.Text) = 0 then
  286.    begin
  287.       EdDay.Color := clWhite;
  288.       BtCreate.Enabled := False;
  289.    end
  290.    else
  291.       if StrToInt(EdDay.Text) = 0 then
  292.       begin
  293.          EdDay.Color := clWhite;
  294.          EdDay.Text := '';
  295.          BtCreate.Enabled := False;
  296.       end
  297.       else
  298.          if Length(EdMonth.Text) > 0 then
  299.          begin
  300.             Num := StrToInt(EdMonth.Text);
  301.             if Num = 2 then
  302.             begin
  303.                if (Length(EdYear.Text) > 0) then
  304.                begin
  305.                   Year := StrToInt(EdYear.Text);
  306.                   if ((Year mod 4 = 0) and not(Year mod 100 = 0)) or (Year mod 400 = 0) then
  307.                      if (StrToInt(EdDay.Text) > (NumOfdays[Num] + 1)) then
  308.                      begin
  309.                         EdDay.Color := $00BBBBFF;
  310.                         BtCreate.Enabled := False;
  311.                      end
  312.                      else
  313.                      begin
  314.                         EdDay.Color := $00CDFF9B;
  315.                         if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  316.                            BtCreate.Enabled := True;
  317.                      end
  318.                   else
  319.                      if (StrToInt(EdDay.Text) > NumOfdays[Num]) then
  320.                      begin
  321.                         EdDay.Color := $00BBBBFF;
  322.                         BtCreate.Enabled := False;
  323.                      end
  324.                      else
  325.                         begin
  326.                            EdDay.Color := $00CDFF9B;
  327.                            if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  328.                               BtCreate.Enabled := True;
  329.                         end
  330.                end
  331.                else
  332.                   if (StrToInt(EdDay.Text) > NumOfdays[Num]) then
  333.                   begin
  334.                      EdDay.Color := $00BBBBFF;
  335.                      BtCreate.Enabled := False;
  336.                   end
  337.                   else
  338.                   begin
  339.                      EdDay.Color := $00CDFF9B;
  340.                      BtCreate.Enabled := False;
  341.                   end;
  342.             end
  343.             else
  344.             begin
  345.                if (StrToInt(EdDay.Text) > NumOfdays[Num]) then
  346.                begin
  347.                   EdDay.Color := $00BBBBFF;
  348.                   BtCreate.Enabled := False;
  349.                end
  350.                else
  351.                begin
  352.                   EdDay.Color := $00CDFF9B;
  353.                   BtCreate.Enabled := False;
  354.                end;
  355.             end;
  356.          end
  357.          else
  358.          begin
  359.             EdDay.Color := $00CDFF9B;
  360.             BtCreate.Enabled := False;
  361.          end;
  362. end;
  363.  
  364. procedure TInputRec.EdDayKeyPress(Sender: TObject; var Key: Char);
  365. const
  366.    IsValid = [#8, #13, '0'..'9'];
  367. begin
  368.    if not (Key in IsValid) then
  369.       Key := #0
  370.    else
  371.    begin
  372.       if Key = #13 then
  373.       begin
  374.          Key := #0;
  375.          if RadioButtonMale.Checked then
  376.             RadioButtonMale.SetFocus;
  377.          if RadioButtonFemale.Checked then
  378.             RadioButtonFemale.SetFocus;
  379.          if (not RadioButtonMale.Checked) and (not RadioButtonFemale.Checked) then
  380.             RadioButtonMale.SetFocus;
  381.       end;
  382.       if (Length(EdDay.Text) = 1) and (Key <> #8) and (StrToInt(EdDay.Text + Key) > 31) then
  383.          Key := #0;
  384.       if (Length(EdDay.Text) = 0) and (Key = '0') then
  385.          Key := #0;
  386.    end;
  387. end;
  388.  
  389. procedure TInputRec.EdMonthChange(Sender: TObject);
  390. var
  391.    TempDay: string;
  392. begin
  393.    if Length(EdMonth.Text) = 0 then
  394.    begin
  395.       EdMonth.Color := clWhite;
  396.       BtCreate.Enabled := False;
  397.    end
  398.    else
  399.       if StrToInt(EdMonth.Text) = 0 then
  400.       begin
  401.          EdMonth.Text := '';
  402.          EdMonth.Color := clWhite;
  403.          BtCreate.Enabled := False;
  404.       end
  405.       else
  406.       begin
  407.          if Length(EdDay.Text) > 0 then
  408.          begin
  409.             TempDay := EdDay.Text;
  410.             EdDay.Text := '';
  411.             EdDay.Text := TempDay;
  412.          end;
  413.          EdMonth.Color := $00CDFF9B;
  414.          if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  415.             BtCreate.Enabled := True;
  416.       end;
  417. end;
  418.  
  419. procedure TInputRec.EdMonthKeyPress(Sender: TObject; var Key: Char);
  420. const
  421.    IsValid = [#8, #13, '0'..'9'];
  422. begin
  423.    if not (Key in IsValid) then
  424.       Key := #0;
  425.    if Key = #13 then
  426.    begin
  427.       Key := #0;
  428.       EdDay.SetFocus;
  429.    end;
  430.    if (Length(EdMonth.Text) = 1) and (Key <> #8) and (StrToInt(EdMonth.Text + Key) > 12) then
  431.       Key := #0;
  432.    if (Length(EdMonth.Text) = 0) and (Key = '0') then
  433.       Key := #0;
  434. end;
  435.  
  436. procedure TInputRec.EdNameChange(Sender: TObject);
  437. const
  438.    SmallLetters = ['a'..'z'];
  439.    BigLetters = ['A'..'Z'];
  440. var
  441.    Temp: string;
  442.    Letter: Char;
  443. begin
  444.    if Length(EdName.Text) > 1 then
  445.    begin
  446.       EdName.Color := $00CDFF9B;
  447.       if (EdSurname.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  448.          BtCreate.Enabled := True;
  449.    end
  450.    else
  451.    begin
  452.       EdName.Color := $00BBBBFF;
  453.       BtCreate.Enabled := False;
  454.    end;
  455.    if EdName.Text <> '' then
  456.    begin
  457.       if EdName.Text[1] in SmallLetters then
  458.       begin
  459.          Temp := Copy(EdName.Text, 2, Length(EdName.Text) - 1);
  460.          Letter := EdName.Text[1];
  461.          Dec(Letter, 32);
  462.          EdName.Text := Letter + Temp;
  463.       end;
  464.       if EdName.Text[2] in BigLetters then
  465.       begin
  466.          Temp := Copy(EdName.Text, 2, Length(EdName.Text) - 1);
  467.          EdName.Text := EdName.Text[1] + LowerCase(Temp);
  468.       end;
  469.    end
  470.    else
  471.    begin
  472.       EdName.Color := clWhite;
  473.       BtCreate.Enabled := False;
  474.    end;
  475. end;
  476.  
  477. procedure TInputRec.EdNameKeyPress(Sender: TObject; var Key: Char);
  478. const
  479.    IsValid = [#8, #13, 'a'..'z', 'A'..'Z'];
  480.    Big = ['A'..'Z'];
  481. begin
  482.    if not (Key in IsValid) then
  483.       Key := #0
  484.    else
  485.    begin
  486.       if Key = #13 then
  487.       begin
  488.          Key := #0;
  489.          EdPatronymic.SetFocus;
  490.       end;
  491.       if (Length(EdName.Text) > 0) and (Key in Big) then
  492.          Inc(Key, 32);
  493.       if (Length(EdName.Text) = 0) and (Key in IsValid) and not(Key in Big) and (Key <> #8) then
  494.          Dec(Key, 32);
  495.       if (Length(EdName.Text) = 0) and (Key = #8) then
  496.          Key := #0;
  497.    end;
  498. end;
  499.  
  500. procedure TInputRec.EdNumberChange(Sender: TObject);
  501. begin
  502.    if Length(EdNumber.Text) = 0 then
  503.    begin
  504.       EdNumber.Color := clWhite;
  505.       BtCreate.Enabled := False;
  506.    end
  507.    else
  508.       if StrToInt(EdNumber.Text) = 0 then
  509.       begin
  510.          EdNumber.Text := '';
  511.          EdNumber.Color := clWhite;
  512.          BtCreate.Enabled := False;
  513.       end
  514.       else
  515.       begin
  516.          if Table.RowCount > 1 then
  517.             if CheckNumber(StrToInt(EdNumber.Text), ComboBoxGroup.Text) then
  518.             begin
  519.                EdNumber.Color := $00CDFF9B;
  520.                if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  521.                   BtCreate.Enabled := True;
  522.             end
  523.             else
  524.             begin
  525.                EdNumber.Color := $00BBBBFF;
  526.                BtCreate.Enabled := False;
  527.             end
  528.          else
  529.          begin
  530.             EdNumber.Color := $00CDFF9B;
  531.             if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  532.                BtCreate.Enabled := True;
  533.          end;
  534.       end;
  535. end;
  536.  
  537. procedure TInputRec.EdNumberKeyPress(Sender: TObject; var Key: Char);
  538. const
  539.    IsValid = [#8, '0'..'9'];
  540. begin
  541.    if Key = #13 then
  542.       begin
  543.          Key := #0;
  544.          EdYear.SetFocus;
  545.       end;
  546.    if not (Key in IsValid) then
  547.       Key := #0;
  548.    if (Length(EdNumber.Text) = 1) and (Key <> #8) and (StrToInt(EdNumber.Text + Key) > 31) then
  549.       Key := #0;
  550.    if (Length(EdNumber.Text) = 0) and (Key = '0') then
  551.       Key := #0;
  552. end;
  553.  
  554. procedure TInputRec.EdPatronymicChange(Sender: TObject);
  555. const
  556.    SmallLetters = ['a'..'z'];
  557.    BigLetters = ['A'..'Z'];
  558. var
  559.    Temp: string;
  560.    Letter: Char;
  561. begin
  562.    if Length(EdPatronymic.Text) > 1 then
  563.    begin
  564.       EdPatronymic.Color := $00CDFF9B;
  565.       if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  566.          BtCreate.Enabled := True;
  567.    end
  568.    else
  569.    begin
  570.       EdPatronymic.Color := $00BBBBFF;
  571.       BtCreate.Enabled := False;
  572.    end;
  573.    if EdPatronymic.Text <> '' then
  574.    begin
  575.       if EdPatronymic.Text[1] in SmallLetters then
  576.       begin
  577.          Temp := Copy(EdPatronymic.Text, 2, Length(EdPatronymic.Text) - 1);
  578.          Letter := EdPatronymic.Text[1];
  579.          Dec(Letter, 32);
  580.          EdPatronymic.Text := Letter + Temp;
  581.       end;
  582.       if EdPatronymic.Text[2] in BigLetters then
  583.       begin
  584.          Temp := Copy(EdPatronymic.Text, 2, Length(EdPatronymic.Text) - 1);
  585.          EdPatronymic.Text := EdPatronymic.Text[1] + LowerCase(Temp);
  586.       end;
  587.    end
  588.    else
  589.    begin
  590.       EdPatronymic.Color := clWhite;
  591.       BtCreate.Enabled := False;
  592.    end;
  593. end;
  594.  
  595. procedure TInputRec.EdPatronymicKeyPress(Sender: TObject; var Key: Char);
  596. const
  597.    IsValid = [#8, #13, 'a'..'z', 'A'..'Z'];
  598.    Big = ['A'..'Z'];
  599. begin
  600.    if not (Key in IsValid) then
  601.       Key := #0
  602.    else
  603.    begin
  604.       if Key = #13 then
  605.       begin
  606.          Key := #0;
  607.          ComboBoxGroup.SetFocus;
  608.       end;
  609.       if (Length(EdPatronymic.Text) > 0) and (Key in Big) then
  610.          Inc(Key, 32);
  611.       if (Length(EdPatronymic.Text) = 0) and (Key in IsValid) and not(Key in Big) and (Key <> #8) then
  612.          Dec(Key, 32);
  613.       if (Length(EdPatronymic.Text) = 0) and (Key = #8) then
  614.          Key := #0;
  615.    end;
  616. end;
  617.  
  618. procedure TInputRec.EdSurnameChange(Sender: TObject);
  619. const
  620.    SmallLetters = ['a'..'z'];
  621.    BigLetters = ['A'..'Z'];
  622. var
  623.    Temp: string;
  624.    Letter: Char;
  625. begin
  626.    if Length(EdSurname.Text) > 1 then
  627.    begin
  628.       EdSurname.Color := $00CDFF9B;
  629.       if (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  630.          BtCreate.Enabled := True;
  631.    end
  632.    else
  633.    begin
  634.       EdSurname.Color := $00BBBBFF;
  635.       BtCreate.Enabled := False;
  636.    end;
  637.    if EdSurname.Text <> '' then
  638.    begin
  639.       if EdSurname.Text[1] in SmallLetters then
  640.       begin
  641.          Temp := Copy(EdSurname.Text, 2, Length(EdSurname.Text) - 1);
  642.          Letter := EdSurname.Text[1];
  643.          Dec(Letter, 32);
  644.          EdSurname.Text := Letter+Temp;
  645.       end;
  646.       if EdSurname.Text[2] in BigLetters then
  647.       begin
  648.          Temp := Copy(EdSurname.Text, 2, Length(EdSurname.Text) - 1);
  649.          EdSurname.Text := EdSurname.Text[1] + LowerCase(Temp);
  650.       end;
  651.    end
  652.    else
  653.    begin
  654.       EdSurname.Color := clWhite;
  655.       BtCreate.Enabled := False;
  656.    end;
  657. end;
  658.  
  659. procedure TInputRec.EdSurnameKeyPress(Sender: TObject; var Key: Char);
  660. const
  661.    IsValid = [#8,#13, 'a'..'z', 'A'..'Z'];
  662.    Big = ['A'..'Z'];
  663. begin
  664.    if not (Key in IsValid) then
  665.       Key := #0
  666.    else
  667.    begin
  668.       if Key = #13 then
  669.       begin
  670.          Key := #0;
  671.          EdName.SetFocus;
  672.       end;
  673.       if (Length(EdSurname.Text) > 0) and (Key in Big) then
  674.          Inc(Key, 32);
  675.       if (Length(EdSurname.Text) = 0) and (Key in IsValid) and not(Key in Big) and (Key <> #8) then
  676.          Dec(Key, 32);
  677.       if (Length(EdSurname.Text) = 0) and (Key = #8) then
  678.          Key := #0;
  679.    end;
  680. end;
  681.  
  682. procedure TInputRec.EdYearChange(Sender: TObject);
  683. var
  684.    TempDay: string;
  685. begin
  686.    if Length(EdYear.Text) = 0 then
  687.    begin
  688.       EdYear.Color := clWhite;
  689.       BtCreate.Enabled := False;
  690.    end
  691.    else
  692.       if (Length(EdYear.Text) = 4) and (StrToInt(EdYear.Text) >= 1964) and (StrToInt(EdYear.Text) <= 2001) then
  693.       begin
  694.          EdYear.Color := $00CDFF9B;
  695.          if Length(EdDay.Text) > 0 then
  696.          begin
  697.             TempDay := EdDay.Text;
  698.             EdDay.Text := '';
  699.             EdDay.Text := TempDay;
  700.          end;
  701.          if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) and (ComboBoxGroup.ItemIndex <> -1) then
  702.             BtCreate.Enabled := True;
  703.       end
  704.       else
  705.       begin
  706.          EdYear.Text := IntToStr(StrToInt(EdYear.Text));
  707.          EdYear.Color := $00BBBBFF;
  708.          BtCreate.Enabled := False;
  709.          if Length(EdDay.Text) > 0 then
  710.          begin
  711.             TempDay := EdDay.Text;
  712.             EdDay.Text := '';
  713.             EdDay.Text := TempDay;
  714.          end;
  715.       end;
  716. end;
  717.  
  718. procedure TInputRec.EdYearKeyPress(Sender: TObject; var Key: Char);
  719. const
  720.    IsValid = [#8, '0'..'9'];
  721. begin
  722.    if Key = #13 then
  723.       begin
  724.          Key := #0;
  725.          EdMonth.SetFocus;
  726.       end;
  727.    if not(Key in IsValid) then
  728.       Key := #0;
  729. end;
  730.  
  731. procedure CleanForm;
  732. var
  733.    i: Integer;
  734. begin
  735.    InputRec.BtReady.Enabled := true;
  736.    InputRec.RadioButtonMale.Checked := false;
  737.    InputRec.RadioButtonFemale.Checked := false;
  738.    InputRec.EdSurname.Clear;
  739.    InputRec.EdName.Clear;
  740.    InputRec.EdPatronymic.Clear;
  741.    InputRec.EdNumber.Clear;
  742.    InputRec.EdYear.Clear;
  743.    InputRec.EdMonth.Clear;
  744.    InputRec.EdDay.Clear;
  745.    InputRec.ComboBoxGroup.ItemIndex := -1;
  746.    for i := 1 to InputRec.Table.RowCount - 1 do
  747.       InputRec.Table.Rows[i].Clear;
  748.    InputRec.Table.RowCount := 1;
  749.    if Length(MainForm.Bufer) > 0 then
  750.    begin
  751.       MainProgMenu.DelRec.Enabled := True;
  752.       MainProgMenu.View.Enabled := True;
  753.    end;
  754. end;
  755.  
  756. procedure TInputRec.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  757. var
  758.    i: Integer;
  759. begin
  760.    if BtReady.Enabled then
  761.       if (Length(EdSurname.Text) > 0) or (Length(EdName.Text) > 0) or (Length(EdPatronymic.Text) > 0) or (Length(EdNumber.Text) > 0) or (Length(EdYear.Text) > 0) or (Length(EdMonth.Text) > 0) or (Length(EdDay.Text) > 0) or RadioButtonMale.Checked or RadioButtonFemale.Checked or (ComboBoxGroup.ItemIndex <> -1) then
  762.          if (Application.MessageBox('Entry fields will be cleaned. Would you like to close the window?','Window closing', MB_OKCANCEL) = mrOk) then
  763.          begin
  764.             CanClose := true;
  765.             CleanForm;
  766.          end
  767.          else
  768.             CanClose := false
  769.       else
  770.          if (Application.MessageBox('Would you like to close the window?','Window closing', MB_OKCANCEL) = mrOk) then
  771.          begin
  772.             BtReady.Enabled := True;
  773.             CanClose := true;
  774.             CleanForm;
  775.          end
  776.          else
  777.             CanClose := false
  778.    else
  779.       if BtCreate.Caption = 'Apply' then
  780.       begin
  781.          if (Application.MessageBox('You haven''t finished changing. Would you like to close the window?','Window closing', MB_OKCANCEL) = mrOk) then
  782.             begin
  783.                BtReady.Enabled := True;
  784.                CanClose := true;
  785.                BtCreate.Caption := 'Create';
  786.                CleanForm;
  787.             end
  788.       end
  789.       else
  790.       begin
  791.          CanClose := true;
  792.          CleanForm;
  793.       end;
  794. end;
  795.  
  796. procedure TInputRec.FormCreate(Sender: TObject);
  797. begin
  798.    Table.Cells[0, 0] := 'Surname';
  799.    Table.Cells[1, 0] := 'Name';
  800.    Table.Cells[2, 0] := 'Patronymic';
  801.    Table.Cells[3, 0] := 'Group';
  802.    Table.Cells[4, 0] := 'Daybook number';
  803.    Table.Cells[5, 0] := 'Date of birth';
  804.    Table.Cells[6, 0] := 'Sex';
  805. end;
  806.  
  807. procedure TInputRec.ImageHelpClick(Sender: TObject);
  808. begin
  809.    Application.MessageBox('Enter all information about the student and click "Create" to create a new record. Data is correct only if background is green.','Help information', MB_OK);
  810. end;
  811.  
  812. function CheckNumberFile(val: Integer; grp: string): Boolean;
  813. var
  814.    i: Integer;
  815.    IsCor: Boolean;
  816. begin
  817.    IsCor := True;
  818.    i := 0;
  819.    repeat
  820.       if grp = IntToStr(MainForm.Bufer[i].Group) then
  821.          if val = MainForm.Bufer[i].Number then
  822.             IsCor := False
  823.          else
  824.             Inc(i)
  825.       else
  826.          Inc(i);
  827.    until (i = Length(MainForm.Bufer)) or not IsCor;
  828.    CheckNumberFile := IsCor;
  829. end;
  830.  
  831. procedure TInputRec.InputFromFileClick(Sender: TObject);
  832. var
  833.    Student: MainForm.TStudent;
  834.    MyFile: file of MainForm.TStudent;
  835. const
  836.    ValidLetters: set of Char = ['A'..'Z', 'a'..'z'];
  837.    ValidNumbers: set of Char = ['0'..'9'];
  838.    MaxYear = 2001;
  839.    MinYear = 1964;
  840. var
  841.    InputFile: file of TStudent;
  842.    StartLen, i, j: Integer;
  843.    IsCorrect, IsCorGroup: Boolean;
  844. begin
  845.    IsCorrect := True;
  846.    if OpenDialog.Execute then
  847.    begin
  848.       AssignFile(InputFile, OpenDialog.FileName);
  849.       Reset(InputFile);
  850.       if Eof(InputFile) then
  851.          MessageDlg('File is empty. Please retry.', mtError, [mbRetry], 0)
  852.       else
  853.       try
  854.          StartLen := Length(MainForm.Bufer);
  855.          SetLength(MainForm.Bufer, StartLen + FileSize(InputFile));
  856.          i := StartLen;
  857.          while (i < Length(MainForm.Bufer)) and IsCorrect do
  858.          begin
  859.             Read(InputFile, MainForm.Bufer[i]);
  860.             Inc(i);
  861.          end;
  862.          if not IsCorrect then
  863.          begin
  864.             SetLength(MainForm.Bufer, StartLen);
  865.             Application.MessageBox('Read error. Please try again.','Input Error', MB_OK);
  866.             CloseFile(InputFile);
  867.          end;
  868.       except
  869.          SetLength(MainForm.Bufer, StartLen);
  870.          Application.MessageBox('Read error. Please try again.','Input Error', MB_OK);
  871.          CloseFile(InputFile);
  872.       end;
  873.    end;
  874.    if IsCorrect then
  875.    begin
  876.       InputRec.Table.RowCount := Length(MainForm.Bufer) + 1;
  877.       for i := StartLen + 1 to Length(MainForm.Bufer) do
  878.       begin
  879.          InputRec.Table.Cells[0,i] := MainForm.Bufer[i-1].Surname;
  880.          InputRec.Table.Cells[1,i] := MainForm.Bufer[i-1].Name;
  881.          InputRec.Table.Cells[2,i] := MainForm.Bufer[i-1].Patronymic;
  882.          InputRec.Table.Cells[3,i] := IntToStr(MainForm.Bufer[i-1].Group);
  883.          InputRec.Table.Cells[4,i] := IntToStr(MainForm.Bufer[i-1].Number);
  884.          InputRec.Table.Cells[5,i] := MainForm.Bufer[i-1].DateOfBirth;
  885.          InputRec.Table.Cells[6,i] := MainForm.Bufer[i-1].Sex;
  886.       end;
  887.    end;
  888. end;
  889.  
  890. procedure TInputRec.RadioButtonFemaleClick(Sender: TObject);
  891. begin
  892.    if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (Length(ComboBoxGroup.Text) > 0) then
  893.       BtCreate.Enabled := True;
  894. end;
  895.  
  896. procedure TInputRec.RadioButtonFemaleKeyPress(Sender: TObject; var Key: Char);
  897. begin
  898.    if (Key = #13) and BtCreate.Enabled then
  899.       BtCreate.SetFocus;
  900. end;
  901.  
  902. procedure TInputRec.RadioButtonMaleClick(Sender: TObject);
  903. begin
  904.    if (EdSurname.Color = $00CDFF9B) and (EdName.Color = $00CDFF9B) and (EdPatronymic.Color = $00CDFF9B) and (EdNumber.Color = $00CDFF9B) and (EdYear.Color = $00CDFF9B) and (EdMonth.Color = $00CDFF9B) and (EdDay.Color = $00CDFF9B) and (Length(ComboBoxGroup.Text) > 0) then
  905.       BtCreate.Enabled := True;
  906. end;
  907.  
  908. procedure TInputRec.RadioButtonMaleKeyPress(Sender: TObject; var Key: Char);
  909. begin
  910.    if (Key = #13) and BtCreate.Enabled then
  911.       BtCreate.SetFocus;
  912. end;
  913.  
  914. procedure TInputRec.TableMouseDown(Sender: TObject; Button: TMouseButton;
  915.   Shift: TShiftState; X, Y: Integer);
  916. var
  917.    ACol: Integer;
  918. begin
  919.    if Button = mbLeft then
  920.    begin
  921.       Table.MouseToCell(x, y, ACol, ChangeRow);
  922.       if (ChangeRow > 0) then
  923.       begin
  924.          BtChange.Enabled := True;
  925.       end;
  926.    end;
  927. end;
  928.  
  929. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement