Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.66 KB | None | 0 0
  1. unit SearchForWindow;
  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.Imaging.pngimage,
  8.   Vcl.ExtCtrls, Vcl.Grids;
  9.  
  10. type
  11.   TSearchFor = class(TForm)
  12.     LblGroup: TLabel;
  13.     LblYear: TLabel;
  14.     EdYear: TEdit;
  15.     RadioButtonMale: TRadioButton;
  16.     RadioButtonFemale: TRadioButton;
  17.     ComboBoxGroup: TComboBox;
  18.     ImageHelp: TImage;
  19.     BtSearch: TButton;
  20.     Table: TStringGrid;
  21.     BtSave: TButton;
  22.     SaveDialog: TSaveDialog;
  23.     procedure ImageHelpClick(Sender: TObject);
  24.     procedure ComboBoxGroupKeyPress(Sender: TObject; var Key: Char);
  25.     procedure EdYearChange(Sender: TObject);
  26.     procedure EdYearKeyPress(Sender: TObject; var Key: Char);
  27.     procedure RadioButtonMaleKeyPress(Sender: TObject; var Key: Char);
  28.     procedure ComboBoxGroupChange(Sender: TObject);
  29.     procedure RadioButtonFemaleKeyPress(Sender: TObject; var Key: Char);
  30.     procedure BtSearchClick(Sender: TObject);
  31.     procedure RadioButtonMaleClick(Sender: TObject);
  32.     procedure RadioButtonFemaleClick(Sender: TObject);
  33.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  34.     procedure BtSaveClick(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.   public
  38.     { Public declarations }
  39.   end;
  40.  
  41. var
  42.   SearchFor: TSearchFor;
  43.  
  44. implementation
  45.  
  46. {$R *.dfm}
  47.  
  48. uses MainForm;
  49.  
  50. procedure TSearchFor.BtSaveClick(Sender: TObject);
  51. var
  52.    OutputFile: file of MainForm.TStudent;
  53.    MyFile: string;
  54.    ButtonSelected: Byte;
  55.    i: Integer;
  56.    Bufer: MainForm.TArr;
  57. begin
  58.    if SaveDialog.Execute then
  59.    begin
  60.       MyFile := SaveDialog.FileName;
  61.       if Pos('.dat', MyFile) <> Length(MyFile) - 3 then
  62.          MyFile := MyFile + '.dat';
  63.       ButtonSelected := 0;
  64.       if FileExists(MyFile) then
  65.          ButtonSelected := MessageDlg('Rewrite file?', mtConfirmation, [mbYes,mbNo], 0);
  66.       if (not FileExists(MyFile)) or (ButtonSelected = mrYes) then
  67.       begin
  68.          SetLength(Bufer, Table.RowCount-1);
  69.          for i := 0 to High(Bufer) do
  70.          begin
  71.             Bufer[i].Surname := Table.Cells[0, i + 1];
  72.             Bufer[i].Name := Table.Cells[1, i + 1];
  73.             Bufer[i].Patronymic := Table.Cells[2, i + 1];
  74.             Bufer[i].Group := StrToInt(Table.Cells[3, i + 1]);
  75.             Bufer[i].Number := StrToInt(Table.Cells[4, i + 1]);
  76.             Bufer[i].DateOfBirth := Table.Cells[5, i + 1];
  77.             Bufer[i].Sex := Table.Cells[6, i + 1];
  78.          end;
  79.          AssignFile(OutputFile, MyFile);
  80.          ReWrite(OutputFile);
  81.          for i := 0 to High(Bufer) do
  82.             Write(OutputFile, Bufer[i]);
  83.          BtSave.Enabled := False;
  84.          Table.Visible := False;
  85.          for i := 0 to Table.RowCount - 1 do
  86.             Table.Rows[i].Clear;
  87.          Table.RowCount := 1;
  88.          SearchFor.Height := 100;
  89.          EdYear.Text := '';
  90.          RadioButtonMale.Checked := false;
  91.          RadioButtonFemale.Checked := false;
  92.          ComboBoxGroup.ItemIndex := -1;
  93.       end;
  94.       CloseFile(OutputFile);
  95.    end;
  96. end;
  97.  
  98. procedure TSearchFor.BtSearchClick(Sender: TObject);
  99. var
  100.    i, Group: Integer;
  101.    Year, Sex: string;
  102. begin
  103.    if Length(ComboBoxGroup.Text) > 0 then
  104.    begin
  105.       if (ComboBoxGroup.ItemIndex = 0) then
  106.          Group := 535;
  107.       if (ComboBoxGroup.ItemIndex = 1) then
  108.          Group := 510;
  109.       if (ComboBoxGroup.ItemIndex = 2) then
  110.          Group := 507;
  111.       if (ComboBoxGroup.ItemIndex = 3) then
  112.          Group := 505;
  113.    end;
  114.    Year := EdYear.Text;
  115.    if RadioButtonMale.Checked then
  116.       Sex := RadioButtonMale.Caption
  117.    else
  118.       Sex := RadioButtonFemale.Caption;
  119.    for i := 0 to High(MainForm.Bufer) do
  120.       if (((MainForm.Bufer[i].Group div 100) mod 1000) = Group) and (Copy(MainForm.Bufer[i].DateOfBirth, Length(MainForm.Bufer[i].DateOfBirth) - 3, 4) = Year) and (MainForm.Bufer[i].Sex = Sex) then
  121.       begin
  122.          Table.RowCount := Table.RowCount + 1;
  123.          Table.Cells[0, Table.RowCount-1] := MainForm.Bufer[i].Surname;
  124.          Table.Cells[1, Table.RowCount-1] := MainForm.Bufer[i].Name;
  125.          Table.Cells[2, Table.RowCount-1] := MainForm.Bufer[i].Patronymic;
  126.          Table.Cells[3, Table.RowCount-1] := IntToStr(MainForm.Bufer[i].Group);
  127.          Table.Cells[4, Table.RowCount-1] := IntToStr(MainForm.Bufer[i].Number);
  128.          Table.Cells[5, Table.RowCount-1] := MainForm.Bufer[i].DateOfBirth;
  129.          Table.Cells[6, Table.RowCount-1] := MainForm.Bufer[i].Sex;
  130.       end;
  131.    if Table.RowCount = 1 then
  132.       Application.MessageBox('Тo students found for given parameters','Result', MB_OK)
  133.    else
  134.    begin
  135.       Table.Cells[0, 0] := 'Surname';
  136.       Table.Cells[1, 0] := 'Name';
  137.       Table.Cells[2, 0] := 'Patronymic';
  138.       Table.Cells[3, 0] := 'Group';
  139.       Table.Cells[4, 0] := 'Daybook number';
  140.       Table.Cells[5, 0] := 'Date of birth';
  141.       Table.Cells[6, 0] := 'Sex';
  142.       Table.Visible := True;
  143.       Table.Height := Table.RowCount * Table.DefaultRowHeight + 15;
  144.       SearchFor.Height := SearchFor.Height + Table.Height + 25;
  145.       BtSave.Enabled := True;
  146.    end;
  147. end;
  148.  
  149. procedure TSearchFor.ComboBoxGroupChange(Sender: TObject);
  150. var
  151.    i: Integer;
  152. begin
  153.    if (EdYear.Color = $00CDFF9B) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) then
  154.       BtSearch.Enabled := True;
  155.    Table.Visible := False;
  156.    for i := 0 to Table.RowCount - 1 do
  157.       Table.Rows[i].Clear;
  158.    Table.RowCount := 1;
  159.    SearchFor.Height := 100;
  160. end;
  161.  
  162. procedure TSearchFor.ComboBoxGroupKeyPress(Sender: TObject; var Key: Char);
  163. begin
  164.    if Key = #13 then
  165.    begin
  166.       Key := #0;
  167.       EdYear.SetFocus;
  168.    end;
  169. end;
  170.  
  171. procedure TSearchFor.EdYearChange(Sender: TObject);
  172. var
  173.    i: Integer;
  174. begin
  175.    if Length(EdYear.Text) = 0 then
  176.    begin
  177.       EdYear.Color := clWhite;
  178.       BtSearch.Enabled := False;
  179.    end
  180.    else
  181.       if (Length(EdYear.Text) = 4) and (StrToInt(EdYear.Text) >= 1964) and (StrToInt(EdYear.Text) <= 2001) then
  182.       begin
  183.          EdYear.Color := $00CDFF9B;
  184.          if (ComboBoxGroup.ItemIndex <> -1) and (RadioButtonMale.Checked or RadioButtonFemale.Checked) then
  185.             BtSearch.Enabled := True;
  186.       end
  187.       else
  188.       begin
  189.          EdYear.Color := $00BBBBFF;
  190.          BtSearch.Enabled := False;
  191.       end;
  192.    Table.Visible := False;
  193.    for i := 0 to Table.RowCount - 1 do
  194.       Table.Rows[i].Clear;
  195.    Table.RowCount := 1;
  196.    SearchFor.Height := 100;
  197. end;
  198.  
  199. procedure TSearchFor.EdYearKeyPress(Sender: TObject; var Key: Char);
  200. const
  201.    IsValid = [#8, '0'..'9'];
  202. begin
  203.    if Key = #13 then
  204.       begin
  205.          Key := #0;
  206.          if RadioButtonFemale.Checked then
  207.             RadioButtonMale.SetFocus
  208.          else
  209.             RadioButtonFemale.SetFocus
  210.       end;
  211.    if not(Key in IsValid) then
  212.       Key := #0;
  213. end;
  214.  
  215. procedure TSearchFor.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  216. var
  217.    i: Integer;
  218. begin
  219.    if (Application.MessageBox('Would you like to close the window?',
  220.       'Windowclosing', MB_OKCANCEL) = mrOk) then
  221.    begin
  222.       CanClose := true;
  223.       Table.Visible := False;
  224.       for i := 0 to Table.RowCount - 1 do
  225.          Table.Rows[i].Clear;
  226.       Table.RowCount := 1;
  227.       SearchFor.Height := 100;
  228.       EdYear.Text := '';
  229.       RadioButtonMale.Checked := false;
  230.       RadioButtonFemale.Checked := false;
  231.       ComboBoxGroup.ItemIndex := -1;
  232.    end
  233.    else
  234.       CanClose := false;
  235. end;
  236.  
  237. procedure TSearchFor.ImageHelpClick(Sender: TObject);
  238. begin
  239.    Application.MessageBox('Choose parameters and click "Find".','Help information', MB_OK);
  240. end;
  241.  
  242. procedure TSearchFor.RadioButtonFemaleClick(Sender: TObject);
  243. var
  244.    i: Integer;
  245. begin
  246.    if (EdYear.Color = $00CDFF9B) and (ComboBoxGroup.ItemIndex <> -1) then
  247.       BtSearch.Enabled := True;
  248.    Table.Visible := False;
  249.    for i := 0 to Table.RowCount - 1 do
  250.       Table.Rows[i].Clear;
  251.    Table.RowCount := 1;
  252.    SearchFor.Height := 100;
  253. end;
  254.  
  255. procedure TSearchFor.RadioButtonFemaleKeyPress(Sender: TObject; var Key: Char);
  256. begin
  257.    if (Key = #13) then
  258.    begin
  259.       if BtSearch.Enabled then
  260.          BtSearch.SetFocus;
  261.    end;
  262. end;
  263.  
  264. procedure TSearchFor.RadioButtonMaleClick(Sender: TObject);
  265. var
  266.    i: Integer;
  267. begin
  268.    if (EdYear.Color = $00CDFF9B) and (ComboBoxGroup.ItemIndex <> -1) then
  269.       BtSearch.Enabled := True;
  270.    Table.Visible := False;
  271.    for i := 0 to Table.RowCount - 1 do
  272.       Table.Rows[i].Clear;
  273.    Table.RowCount := 1;
  274.    SearchFor.Height := 100;
  275. end;
  276.  
  277. procedure TSearchFor.RadioButtonMaleKeyPress(Sender: TObject; var Key: Char);
  278. begin
  279.    if (Key = #13) then
  280.    begin
  281.       if BtSearch.Enabled then
  282.          BtSearch.SetFocus;
  283.    end;
  284. end;
  285.  
  286. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement