Advertisement
gguuppyy

ModifyCandidateUnit

May 4th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. Unit ModifyCandidateUnit;
  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.StdCtrls, Vcl.Grids,
  9. CandidateListUnit, PositiveNumberComponentUnit, NameComponentUnit;
  10.  
  11. Type
  12. TModifyCandidateForm = Class(TForm)
  13. TitleLabel: TLabel;
  14.  
  15. DistrictNumberLabel: TLabel;
  16. DistrictNumberEdit: TEdit;
  17. LastNameLabel: TLabel;
  18. LastNameEdit: TEdit;
  19. FirstNameLabel: TLabel;
  20. FirstNameEdit: TEdit;
  21. PatronymicLabel: TLabel;
  22. PatronymicEdit: TEdit;
  23. PartyLabel: TLabel;
  24. PartyEdit: TEdit;
  25. AgeLabel: TLabel;
  26. AgeEdit: TEdit;
  27. ProfessionLabel: TLabel;
  28. ProfessionEdit: TEdit;
  29. ModifyButton: TButton;
  30. CancelButton: TButton;
  31.  
  32. Procedure CreateParams(Var Params: TCreateParams); override;
  33. Function FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  34. Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  35. Procedure FormShow(Sender: TObject);
  36.  
  37. Procedure EditChange(Sender: TObject);
  38. Procedure ControlKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  39.  
  40. Procedure PositiveNumberEditContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  41. Procedure PositiveNumberEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  42. Procedure PositiveNumberEditKeyPress(Sender: TObject; Var Key: Char);
  43. Procedure PositiveNumberEditKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
  44.  
  45. Procedure NameEditContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  46. Procedure NameEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  47. Procedure NameEditKeyPress(Sender: TObject; Var Key: Char);
  48. Procedure NameEditKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
  49.  
  50. Procedure ModifyButtonClick(Sender: TObject);
  51. Procedure CancelButtonClick(Sender: TObject);
  52.  
  53. Private
  54. { Private declarations }
  55. Public
  56. { Public declarations }
  57. End;
  58.  
  59. Var
  60. ModifyCandidateForm: TModifyCandidateForm;
  61.  
  62. Implementation
  63.  
  64. {$R *.dfm}
  65.  
  66. Uses
  67. MenuUnit, ViewCandidateListUnit;
  68.  
  69. Procedure SetCandidateData(OldCandidateData: TCandidateData);
  70. Begin
  71. With ModifyCandidateForm, OldCandidateData Do
  72. Begin
  73. DistrictNumberEdit.Text := IntToStr(DistrictNumber);
  74. LastNameEdit.Text := String(LastName);
  75. FirstNameEdit.Text := String(FirstName);
  76. PatronymicEdit.Text := String(Patronymic);
  77. PartyEdit.Text := String(Party);
  78. AgeEdit.Text := IntToStr(Age);
  79. ProfessionEdit.Text := String(Profession);
  80. End;
  81. End;
  82.  
  83. Function GetCandidateData(): TCandidateData;
  84. Var
  85. NewCandidateData: TCandidateData;
  86. Begin
  87. With NewCandidateData, ModifyCandidateForm Do
  88. Begin
  89. DistrictNumber := StrToInt(DistrictNumberEdit.Text);
  90. LastName := TName(Trim(LastNameEdit.Text));
  91. FirstName := TName(Trim(FirstNameEdit.Text));
  92. Patronymic := TName(Trim(PatronymicEdit.Text));
  93. Party := TPartyName(Trim(PartyEdit.Text));
  94. Age := StrToInt(AgeEdit.Text);
  95. Profession := TProfessionName(Trim(ProfessionEdit.Text));
  96. End;
  97. GetCandidateData := NewCandidateData;
  98. End;
  99.  
  100.  
  101. Procedure TModifyCandidateForm.CreateParams(Var Params: TCreateParams);
  102. Begin
  103. Inherited;
  104. Params.ExStyle := Params.ExStyle Or WS_EX_APPWINDOW;
  105. End;
  106.  
  107. Function TModifyCandidateForm.FormHelp(Command: Word; Data: NativeInt; Var CallHelp: Boolean): Boolean;
  108. Begin
  109. CallHelp := False;
  110. FormHelp := False;
  111. End;
  112.  
  113. Procedure TModifyCandidateForm.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  114. Begin
  115. If Key = VK_ESCAPE Then
  116. Close;
  117. End;
  118.  
  119. Procedure TModifyCandidateForm.FormShow(Sender: TObject);
  120. Begin
  121. TitleLabel.Left := (ClientWidth - TitleLabel.Width) Div 2;
  122. If StateForm = Edit Then
  123. SetCandidateData(OldCandidateData);
  124. End;
  125.  
  126.  
  127. Function IsFullFields(): Boolean;
  128. Begin
  129. With ModifyCandidateForm Do
  130. IsFullFields := (Trim(DistrictNumberEdit.Text) <> '') And
  131. (Trim(LastNameEdit.Text) <> '') And
  132. (Trim(FirstNameEdit.Text) <> '') And
  133. (Trim(PartyEdit.Text) <> '') And
  134. (Trim(AgeEdit.Text) <> '') And
  135. (Trim(ProfessionEdit.Text) <> '');
  136. End;
  137.  
  138. Procedure TModifyCandidateForm.EditChange(Sender: TObject);
  139. Var
  140. NewCandidateData: TCandidateData;
  141. Begin
  142. ModifyButton.Enabled := False;
  143. If IsFullFields() Then
  144. Begin
  145. NewCandidateData := GetCandidateData();
  146. ModifyButton.Enabled := Not IsExistCandidate(CandidateList, NewCandidateData);
  147. End;
  148. End;
  149.  
  150. Procedure TModifyCandidateForm.ControlKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  151. Begin
  152. If Key = VK_UP Then
  153. SelectNext(TWinControl(Sender), False, True)
  154. Else If Key = VK_DOWN Then
  155. SelectNext(TWinControl(Sender), True, True)
  156. Else If (Key = Ord(ENTER)) And (ModifyButton.Enabled) Then
  157. ModifyButtonClick(ModifyButton);
  158. End;
  159.  
  160.  
  161. Procedure TModifyCandidateForm.PositiveNumberEditContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  162. Var
  163. Edit: TEdit;
  164. Max: Integer;
  165. Begin
  166. Edit := TEdit(Sender);
  167. If Edit = DistrictNumberEdit Then
  168. Max := MAX_DISTRICT_NUMBER
  169. Else
  170. Max := MAX_AGE;
  171. PositiveNumberComponentContextPopup(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Handled);
  172. End;
  173.  
  174. Procedure TModifyCandidateForm.PositiveNumberEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  175. Var
  176. Edit: TEdit;
  177. Max: Integer;
  178. Begin
  179. Edit := TEdit(Sender);
  180. If Edit = DistrictNumberEdit Then
  181. Max := MAX_DISTRICT_NUMBER
  182. Else
  183. Max := MAX_AGE;
  184. PositiveNumberComponentKeyDown(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Key, Shift);
  185. ControlKeyDown(Sender, Key, Shift);
  186. End;
  187.  
  188. Procedure TModifyCandidateForm.PositiveNumberEditKeyPress(Sender: TObject; Var Key: Char);
  189. Var
  190. Edit: TEdit;
  191. Max: Integer;
  192. Begin
  193. Edit := TEdit(Sender);
  194. If Edit = DistrictNumberEdit Then
  195. Max := MAX_DISTRICT_NUMBER
  196. Else
  197. Max := MAX_AGE;
  198. PositiveNumberComponentKeyPress(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Key);
  199. End;
  200.  
  201. Procedure TModifyCandidateForm.PositiveNumberEditKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
  202. Begin
  203. PositiveNumberComponentKeyUp();
  204. End;
  205.  
  206.  
  207. Procedure TModifyCandidateForm.NameEditContextPopup(Sender: TObject; MousePos: TPoint; Var Handled: Boolean);
  208. Var
  209. Edit: TEdit;
  210. Max: Integer;
  211. Begin
  212. Edit := TEdit(Sender);
  213. If (Edit = LastNameEdit) Or (Edit = FirstNameEdit) Or (Edit = PatronymicEdit) Then
  214. Max := MAX_NAME_LENGTH
  215. Else If (Edit = PartyEdit) Then
  216. Max := MAX_PARTY_NAME_LENGTH
  217. Else
  218. Max := MAX_PROFESSION_NAME_LENGTH;
  219. NameComponentContextPopup(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Handled);
  220. End;
  221.  
  222. Procedure TModifyCandidateForm.NameEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
  223. Var
  224. Edit: TEdit;
  225. Max: Integer;
  226. Begin
  227. Edit := TEdit(Sender);
  228. If (Edit = LastNameEdit) Or (Edit = FirstNameEdit) Or (Edit = PatronymicEdit) Then
  229. Max := MAX_NAME_LENGTH
  230. Else If (Edit = PartyEdit) Then
  231. Max := MAX_PARTY_NAME_LENGTH
  232. Else
  233. Max := MAX_PROFESSION_NAME_LENGTH;
  234. NameComponentKeyDown(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Key, Shift);
  235. ControlKeyDown(Sender, Key, Shift);
  236. End;
  237.  
  238. Procedure TModifyCandidateForm.NameEditKeyPress(Sender: TObject; Var Key: Char);
  239. Var
  240. Edit: TEdit;
  241. Max: Integer;
  242. Begin
  243. Edit := TEdit(Sender);
  244. If (Edit = LastNameEdit) Or (Edit = FirstNameEdit) Or (Edit = PatronymicEdit) Then
  245. Max := MAX_NAME_LENGTH
  246. Else If (Edit = PartyEdit) Then
  247. Max := MAX_PARTY_NAME_LENGTH
  248. Else
  249. Max := MAX_PROFESSION_NAME_LENGTH;
  250. NameComponentKeyPress(Edit.Text, Edit.SelStart, Edit.SelLength, Max, Key);
  251. End;
  252.  
  253. Procedure TModifyCandidateForm.NameEditKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
  254. Begin
  255. NameComponentKeyUp();
  256. End;
  257.  
  258.  
  259. Procedure TModifyCandidateForm.ModifyButtonClick(Sender: TObject);
  260. Var
  261. NewCandidateData: TCandidateData;
  262. Begin
  263. NewCandidateData := GetCandidateData();
  264. If StateForm = Add Then
  265. AddCandidate(CandidateList, NewCandidateData)
  266. Else
  267. EditCandidate(CandidateList, OldCandidateData, NewCandidateData);
  268. IsModified := True;
  269. Close;
  270. End;
  271.  
  272. Procedure TModifyCandidateForm.CancelButtonClick(Sender: TObject);
  273. Begin
  274. Close;
  275. End;
  276.  
  277. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement