Advertisement
gguuppyy

ViewCandidateListUnit

May 4th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. Unit ViewCandidateListUnit;
  2.  
  3. Interface
  4.  
  5. Uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7. System.Classes, Vcl.Graphics,
  8. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Grids,
  9. Vcl.ExtDlgs,
  10. ModifyCandidateUnit, CandidateListUnit, FileUnit;
  11.  
  12. Type
  13. TViewCandidateListForm = Class(TForm)
  14. MainMenu: TMainMenu;
  15. FileMenuItem: TMenuItem;
  16. OpenMenuItem: TMenuItem;
  17. SaveMenuItem: TMenuItem;
  18.  
  19. OpenTextFileDialog: TOpenTextFileDialog;
  20. SaveTextFileDialog: TSaveTextFileDialog;
  21.  
  22. AddButton: TButton;
  23. EditButton: TButton;
  24. DeleteButton: TButton;
  25. ContentEdit: TEdit;
  26. CandidateListStringGrid: TStringGrid;
  27.  
  28. Procedure CreateParams(Var Params: TCreateParams); Override;
  29. Procedure FormCreate(Sender: TObject);
  30. Function FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  31. Procedure FormShow(Sender: TObject);
  32.  
  33. Procedure OpenMenuItemClick(Sender: TObject);
  34. Procedure SaveMenuItemClick(Sender: TObject);
  35.  
  36. Procedure AddButtonClick(Sender: TObject);
  37. Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  38. Procedure EditButtonClick(Sender: TObject);
  39. Procedure CandidateListStringGridDblClick(Sender: TObject);
  40. Procedure DeleteButtonClick(Sender: TObject);
  41. Procedure CandidateListStringGridKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  42.  
  43. Procedure CandidateListStringGridSelectCell(Sender: TObject; ACol, ARow: Integer; Var CanSelect: Boolean);
  44.  
  45. Private
  46. { Private declarations }
  47. Public
  48. { Public declarations }
  49. End;
  50.  
  51. Var
  52. ViewCandidateListForm: TViewCandidateListForm;
  53. OldCandidateData: TCandidateData;
  54. IsModified: Boolean = False;
  55.  
  56. Implementation
  57.  
  58. {$R *.dfm}
  59.  
  60. Uses
  61. MenuUnit;
  62.  
  63. Procedure TViewCandidateListForm.CreateParams(Var Params: TCreateParams);
  64. Begin
  65. Inherited;
  66. Params.ExStyle := Params.ExStyle Or WS_EX_APPWINDOW;
  67. End;
  68.  
  69. Procedure TViewCandidateListForm.FormCreate(Sender: TObject);
  70. Begin
  71. With CandidateListStringGrid Do
  72. Begin
  73. Cells[0, 0] := '№ округа';
  74. Cells[1, 0] := 'Фамилия';
  75. Cells[2, 0] := 'Имя';
  76. Cells[3, 0] := 'Отчество';
  77. Cells[4, 0] := 'Партия';
  78. Cells[5, 0] := 'Возраст';
  79. Cells[6, 0] := 'Профессия';
  80. End;
  81. End;
  82.  
  83. Function TViewCandidateListForm.FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  84. Begin
  85. CallHelp := False;
  86. FormHelp := False;
  87. End;
  88.  
  89. Procedure TViewCandidateListForm.FormShow(Sender: TObject);
  90. Var
  91. CandidateCount: Integer;
  92. Begin
  93. CandidateCount := CountCandidates(CandidateList);
  94. CandidateListStringGrid.RowCount := CountCandidates(CandidateList) + 1;
  95. SaveMenuItem.Enabled := CandidateCount > 0;
  96. FillCandidateListStringGrid(CandidateList, CandidateListStringGrid);
  97. End;
  98.  
  99. Procedure TViewCandidateListForm.OpenMenuItemClick(Sender: TObject);
  100. Var
  101. InputFile: TCandidateDataFile;
  102. TempCandidateList: PCandidate;
  103. IsCorrect: Boolean;
  104. Begin
  105. If OpenTextFileDialog.Execute Then
  106. Begin
  107. AssignFile(InputFile, OpenTextFileDialog.FileName);
  108. TempCandidateList := Nil;
  109. IsCorrect := ReadFileData(InputFile, TempCandidateList);
  110. If IsCorrect Then
  111. Begin
  112. ClearCandidates(CandidateList);
  113. RecordFileData(CandidateList, TempCandidateList, CandidateListStringGrid);
  114. IsSaved := True;
  115. SaveMenuItem.Enabled := True;
  116. End
  117. Else
  118. Begin
  119. ClearCandidates(TempCandidateList);
  120. Application.MessageBox('Содержимое файла повреждено!', 'Ошибка', MB_OK + MB_ICONERROR);
  121. End;
  122. End;
  123. End;
  124.  
  125. Procedure TViewCandidateListForm.SaveMenuItemClick(Sender: TObject);
  126. Var
  127. OutputFile: TCandidateDataFile;
  128. Begin
  129. If SaveTextFileDialog.Execute Then
  130. Begin
  131. AssignFile(OutputFile, SaveTextFileDialog.FileName);
  132. WriteFileData(OutputFile, CandidateList);
  133. IsSaved := True;
  134. End;
  135. End;
  136.  
  137. Function GetCandidateData(CandidateListStringGrid: TStringGrid): TCandidateData;
  138. Var
  139. OldCandidateData: TCandidateData;
  140. Begin
  141. With OldCandidateData, CandidateListStringGrid Do
  142. Begin
  143. DistrictNumber := StrToInt(Cells[0, Row]);
  144. LastName := TName(Cells[1, Row]);
  145. FirstName := TName(Cells[2, Row]);
  146. Patronymic := TName(Cells[3, Row]);
  147. Party := TPartyName(Cells[4, Row]);
  148. Age := StrToInt(Cells[5, Row]);
  149. Profession := TProfessionName(Cells[6, Row]);
  150. End;
  151. GetCandidateData := OldCandidateData;
  152. End;
  153.  
  154. Procedure CreateModifyForm(Const TitleLabelCaption, ModifyButtonCaption: String);
  155. Begin
  156. ModifyCandidateForm := TModifyCandidateForm.Create(ViewCandidateListForm);
  157. ModifyCandidateForm.Icon := ViewCandidateListForm.Icon;
  158. ModifyCandidateForm.TitleLabel.Caption := TitleLabelCaption;
  159. ModifyCandidateForm.ModifyButton.Caption := ModifyButtonCaption;
  160. ModifyCandidateForm.ShowModal;
  161. End;
  162.  
  163. Procedure TViewCandidateListForm.AddButtonClick(Sender: TObject);
  164. Begin
  165. If CountCandidates(CandidateList) < MAX_CANDIDATE_COUNT Then
  166. Begin
  167. StateForm := Add;
  168. CreateModifyForm('Добавление кандидата', 'Добавить');
  169. If IsModified Then
  170. Begin
  171. CandidateListStringGrid.RowCount := CandidateListStringGrid.RowCount + 1;
  172. FillCandidateListStringGrid(CandidateList, CandidateListStringGrid);
  173. IsModified := False;
  174. IsSaved := False;
  175. SaveMenuItem.Enabled := True;
  176. ContentEdit.Text := '';
  177. End;
  178. End
  179. Else
  180. Application.MessageBox(PWideChar('Количество кандидатов не может превышать ' + IntToStr(MAX_CANDIDATE_COUNT) + ' !'),
  181. 'Ïðåäóïðåæäåíèå', MB_OK + MB_ICONWARNING);
  182. End;
  183.  
  184. Procedure TViewCandidateListForm.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  185. Begin
  186. If Key = VK_INSERT Then
  187. AddButtonClick(AddButton)
  188. Else
  189. If Key = VK_ESCAPE Then
  190. Close;
  191. End;
  192.  
  193. Procedure TViewCandidateListForm.EditButtonClick(Sender: TObject);
  194. Begin
  195. If CandidateListStringGrid.Row > 0 Then
  196. Begin
  197. StateForm := Edit;
  198. OldCandidateData := GetCandidateData(CandidateListStringGrid);
  199. CreateModifyForm('Изменение кандидата', 'Изменить');
  200. If IsModified Then
  201. Begin
  202. FillCandidateListStringGrid(CandidateList, CandidateListStringGrid);
  203. IsModified := False;
  204. IsSaved := False;
  205. SaveMenuItem.Enabled := True;
  206. ContentEdit.Text := '';
  207. End;
  208. End
  209. Else
  210. Application.MessageBox('Не выбрано редактируемое поле!', 'Предупреждение', MB_OK + MB_ICONWARNING);
  211. End;
  212.  
  213. Procedure TViewCandidateListForm.CandidateListStringGridDblClick(Sender: TObject);
  214. Begin
  215. EditButtonClick(EditButton);
  216. End;
  217.  
  218. Procedure TViewCandidateListForm.DeleteButtonClick(Sender: TObject);
  219. Var
  220. Confirmation, CandidateCount: Integer;
  221. Begin
  222. If CandidateListStringGrid.Row > 0 Then
  223. Begin
  224. Confirmation := Application.MessageBox('Вы действительно хотите удалить кандидата?', 'Удаление кандидата',
  225. MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2);
  226. If Confirmation = IDYES Then
  227. Begin
  228. DeleteCandidate(CandidateList, GetCandidateData(CandidateListStringGrid));
  229. CandidateListStringGrid.RowCount := CandidateListStringGrid.RowCount - 1;
  230. FillCandidateListStringGrid(CandidateList, CandidateListStringGrid);
  231. CandidateCount := CountCandidates(CandidateList);
  232. IsSaved := CandidateCount = 0;
  233. SaveMenuItem.Enabled := CandidateCount > 0;
  234. ContentEdit.Text := '';
  235. End;
  236. End
  237. Else
  238. Application.MessageBox('Не выбрано редактируемое поле!', 'Предупреждение', MB_OK + MB_ICONWARNING);
  239. End;
  240.  
  241. Procedure TViewCandidateListForm.CandidateListStringGridKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  242. Begin
  243. If Key = VK_DELETE Then
  244. DeleteButtonClick(DeleteButton);
  245. End;
  246.  
  247. Procedure TViewCandidateListForm.CandidateListStringGridSelectCell(Sender: TObject; ACol, ARow: Integer; Var CanSelect: Boolean);
  248. Begin
  249. If ARow > 0 Then
  250. ContentEdit.Text := CandidateListStringGrid.Cells[ACol, ARow]
  251. Else
  252. ContentEdit.Text := '';
  253. End;
  254.  
  255. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement