Advertisement
THOMAS_SHELBY_18

ascascasc

Feb 17th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. unit AddRecordUnit;
  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.Mask, Vcl.StdCtrls, Vcl.Menus, Clipbrd;
  8.  
  9. type
  10. TAddRecordForm = class(TForm)
  11. HardwareNameEdit: TEdit;
  12. CompanyNameEdit: TEdit;
  13. DescriptionEdit: TEdit;
  14. WarrantyEdit: TEdit;
  15. HardwareNameLabel: TLabel;
  16. TitleLabel: TLabel;
  17. CompanyNameLabel: TLabel;
  18. DescriptionLabel: TLabel;
  19. WarrantyLabel: TLabel;
  20. PriceLabel: TLabel;
  21. AddButton: TButton;
  22. CancelButton: TButton;
  23. PriceEdit: TEdit;
  24. CopyPastePopupMenu: TPopupMenu;
  25. CopyButton: TMenuItem;
  26. PasteButton: TMenuItem;
  27. CutButton: TMenuItem;
  28. procedure CancelButtonClick(Sender: TObject);
  29. procedure AddButtonClick(Sender: TObject);
  30. procedure CopyButtonClick(Sender: TObject);
  31. procedure PasteButtonClick(Sender: TObject);
  32. procedure CutButtonClick(Sender: TObject);
  33. procedure CopyPastePopupMenuPopup(Sender: TObject);
  34. procedure StrEditChange(Sender: TObject);
  35. procedure WarrantyEditChange(Sender: TObject);
  36. procedure PriceEditChange(Sender: TObject);
  37. procedure EditKeyDown(Sender: TObject; var Key: Word;
  38. Shift: TShiftState);
  39. procedure WarrantyEditKeyPress(Sender: TObject; var Key: Char);
  40. procedure PriceEditKeyPress(Sender: TObject; var Key: Char);
  41. procedure EditDblClick(Sender: TObject);
  42. private
  43. { Private declarations }
  44. public
  45. { Public declarations }
  46. end;
  47.  
  48. var
  49. AddRecordForm: TAddRecordForm;
  50.  
  51. const
  52. kBACKSPACE = #8;
  53. kMINUS = #45;
  54. kCOMMA = #44;
  55. kDOWN = 40;
  56. kUP = 38;
  57. kENTER = 13;
  58. kINSERT = 45;
  59.  
  60. implementation
  61. uses MainUnit;
  62. {$R *.dfm}
  63.  
  64. procedure ClearAddRecordForm;
  65. begin
  66. With AddRecordForm Do
  67. Begin
  68. HardwareNameEdit.Text := '';
  69. CompanyNameEdit.Text := '';
  70. DescriptionEdit.Text := '';
  71. WarrantyEdit.Text := '';
  72. PriceEdit.Text := '';
  73. End;
  74. end;
  75.  
  76. Function CreateNewRecord: THardwareRecord;
  77. Var
  78. NewHardwareRecord: THardwareRecord;
  79. Begin
  80. With NewHardwareRecord, AddRecordForm Do
  81. Begin
  82. HardwareName := HardwareNameEdit.Text;
  83. CompanyName := CompanyNameEdit.Text;
  84. Description := DescriptionEdit.Text;
  85. Warranty := StrToInt(WarrantyEdit.Text);
  86. Price := Int(StrToFloat(PriceEdit.Text) * 100) / 100;
  87. End;
  88. CreateNewRecord := NewHardwareRecord;
  89. End;
  90.  
  91. Procedure AddRecordToGrid(NewHardwareRecord: THardwareRecord);
  92. Begin
  93. With MainForm.StringGrid, NewHardwareRecord Do
  94. Begin
  95. if IsStringGridNotEmpty then
  96. RowCount := RowCount + 1
  97. else
  98. IsStringGridNotEmpty := True;
  99.  
  100. Cells[0, RowCount-1] := HardwareName;
  101. Cells[1, RowCount-1] := CompanyName;
  102. Cells[2, RowCount-1] := Description;
  103. Cells[3, RowCount-1] := IntToStr(Warranty) + ' мес';
  104. Cells[4, RowCount-1] := FloatToStr(Price);
  105. Cells[DelColomn, RowCount-1] := ' X';
  106. Cells[EditColomn, RowCount-1] := ' ...';
  107. End;
  108. End;
  109.  
  110. procedure TAddRecordForm.AddButtonClick(Sender: TObject);
  111. Var
  112. NewHardwareRecord: THardwareRecord;
  113. begin
  114. NewHardwareRecord := CreateNewRecord();
  115. ClearAddRecordForm;
  116. AddRecordToGrid(NewHardwareRecord);
  117. AddRecordForm.Close;
  118. end;
  119.  
  120. procedure TAddRecordForm.CancelButtonClick(Sender: TObject);
  121. begin
  122. AddRecordForm.Close;
  123. ClearAddRecordForm;
  124. end;
  125.  
  126.  
  127. ////////////////////////////POPUP MENU /////////////////////////////////////
  128. procedure TAddRecordForm.CopyButtonClick(Sender: TObject);
  129. begin
  130. TEdit(ActiveControl).CopyToClipboard;
  131. end;
  132.  
  133. procedure TAddRecordForm.CopyPastePopupMenuPopup(Sender: TObject);
  134. var
  135. IValue: Integer;
  136. FlValue: Single;
  137. Buffer: String;
  138. begin
  139. Buffer := Clipboard.AsText;
  140. PasteButton.Enabled := True;
  141.  
  142. if ActiveControl.Name = 'WarrantyEdit' then
  143. PasteButton.Enabled := TryStrToInt(Buffer, IValue)
  144. else
  145. if ActiveControl.Name = 'PriceEdit' then
  146. PasteButton.Enabled := TryStrToFloat(Buffer, FlValue);
  147. end;
  148.  
  149. procedure TAddRecordForm.CutButtonClick(Sender: TObject);
  150. begin
  151. TEdit(ActiveControl).CutToClipboard;
  152. end;
  153.  
  154. Function CheckPriceEdit():Boolean;
  155. var
  156. IsEditCorrect: Boolean;
  157. FlValue: Single;
  158. Begin
  159. with AddRecordForm.PriceEdit do
  160. begin
  161. IsEditCorrect := (Text <> '') And (TryStrToFloat(Text, FlValue) And (Text[1] <> ',') And (Text[High(Text)] <> ','));
  162. end;
  163. CheckPriceEdit := IsEditCorrect;
  164. End;
  165.  
  166. Procedure EditAddButtonEnabled;
  167. var
  168. IsRecordCorrect: Boolean;
  169. Begin
  170. with AddRecordForm do
  171. begin
  172. IsRecordCorrect := (HardwareNameEdit.Text <> '') And (CompanyNameEdit.Text <> '') And (DescriptionEdit.Text <> '') And (WarrantyEdit.Text <> '') And CheckPriceEdit;
  173. AddRecordForm.AddButton.Enabled := IsRecordCorrect;
  174. end;
  175. End;
  176.  
  177. procedure TAddRecordForm.StrEditChange(Sender: TObject);
  178. begin
  179. EditAddButtonEnabled;
  180. end;
  181.  
  182. procedure TAddRecordForm.WarrantyEditChange(Sender: TObject);
  183. var
  184. CursPos: Byte;
  185. TempStr: String;
  186. IValue: Integer;
  187. begin
  188. with WarrantyEdit do
  189. begin
  190. CursPos := SelStart;
  191. TempStr := Text;
  192.  
  193. if not TryStrToInt(TempStr, IValue) or (IValue < 0) then
  194. begin
  195. Delete (TempStr, SelStart, 1);
  196. Text := TempStr;
  197. SelStart := CursPos-1;
  198. end
  199. else
  200. begin
  201. Text := IntToStr(IValue);
  202. SelStart := CursPos;
  203. end;
  204. end;
  205.  
  206. EditAddButtonEnabled;
  207. end;
  208.  
  209. procedure TAddRecordForm.PriceEditChange(Sender: TObject);
  210. var
  211. TempStr: String;
  212. CursPos: Byte;
  213. begin
  214. with PriceEdit do
  215. begin
  216. TempStr := Text;
  217. CursPos := SelStart;
  218.  
  219. if (length(TempStr)>1) And (TempStr[1] = '0') And (TempStr[2] = '0') then
  220. begin
  221. Delete(TempStr, 2, 1);
  222. Text := TempStr;
  223. SelStart := CursPos-1;
  224. end;
  225. end;
  226.  
  227. EditAddButtonEnabled;
  228. end;
  229.  
  230. procedure TAddRecordForm.EditKeyDown(Sender: TObject; var Key: Word;
  231. Shift: TShiftState);
  232. Begin
  233. case key of
  234. kENTER, kDOWN:
  235. begin
  236. SelectNext(ActiveControl, True, True);
  237. with TEdit(ActiveControl) do
  238. SelStart := Length(Text);
  239. end;
  240. kUP:
  241. begin
  242. SelectNext(ActiveControl, False, True);
  243. with TEdit(ActiveControl) do
  244. SelStart := Length(Text);
  245. end;
  246. kINSERT:
  247. Key := 0;
  248. end;
  249. End;
  250.  
  251. procedure TAddRecordForm.EditDblClick(Sender: TObject);
  252. begin
  253. TEdit(Sender).Text := '';
  254. end;
  255.  
  256. procedure TAddRecordForm.WarrantyEditKeyPress(Sender: TObject; var Key: Char);
  257. begin
  258. if Not (Key in ['0'..'9', kBACKSPACE]) then
  259. Key := #0;
  260. end;
  261.  
  262. procedure TAddRecordForm.PriceEditKeyPress(Sender: TObject; var Key: Char);
  263. begin
  264. if Not (Key in ['0'..'9', kBACKSPACE, kCOMMA]) then
  265. Key := #0;
  266. end;
  267.  
  268. procedure TAddRecordForm.PasteButtonClick(Sender: TObject);
  269. var
  270. CursPos: Byte;
  271. TempStr: String;
  272. ActiveEdit: TEdit;
  273. IValue: Integer;
  274. FlValue: Single;
  275. begin
  276. ActiveEdit := TEdit(ActiveControl);
  277. with ActiveEdit do
  278. begin
  279. CursPos := SelStart;
  280. TempStr := Text;
  281.  
  282. PasteFromClipboard;
  283.  
  284. if ActiveControl.Name = 'WarrantyEdit' then
  285. begin
  286. if not TryStrToInt(Text, IValue) or (IValue < 0) then
  287. begin
  288. Text := TempStr;
  289. SelStart := CursPos;
  290. end;
  291. end
  292. else
  293. if ActiveControl.Name = 'PriceEdit' then
  294. begin
  295. if not TryStrToFloat(Text, FlValue) or (FlValue < 0) then
  296. begin
  297. Text := TempStr;
  298. SelStart := CursPos;
  299. end
  300. end;
  301. End;
  302. end;
  303.  
  304. ///////////////////////////////////////////////////////////////////////////////
  305.  
  306. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement