Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit AddRecordUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Mask, Vcl.StdCtrls, Vcl.Menus, Clipbrd;
- type
- TAddRecordForm = class(TForm)
- HardwareNameEdit: TEdit;
- CompanyNameEdit: TEdit;
- DescriptionEdit: TEdit;
- WarrantyEdit: TEdit;
- HardwareNameLabel: TLabel;
- TitleLabel: TLabel;
- CompanyNameLabel: TLabel;
- DescriptionLabel: TLabel;
- WarrantyLabel: TLabel;
- PriceLabel: TLabel;
- AddButton: TButton;
- CancelButton: TButton;
- PriceEdit: TEdit;
- CopyPastePopupMenu: TPopupMenu;
- CopyButton: TMenuItem;
- PasteButton: TMenuItem;
- CutButton: TMenuItem;
- procedure CancelButtonClick(Sender: TObject);
- procedure AddButtonClick(Sender: TObject);
- procedure CopyButtonClick(Sender: TObject);
- procedure PasteButtonClick(Sender: TObject);
- procedure CutButtonClick(Sender: TObject);
- procedure CopyPastePopupMenuPopup(Sender: TObject);
- procedure StrEditChange(Sender: TObject);
- procedure WarrantyEditChange(Sender: TObject);
- procedure PriceEditChange(Sender: TObject);
- procedure EditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure WarrantyEditKeyPress(Sender: TObject; var Key: Char);
- procedure PriceEditKeyPress(Sender: TObject; var Key: Char);
- procedure EditDblClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure StrEditKeyPress(Sender: TObject; var Key: Char);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- AddRecordForm: TAddRecordForm;
- const
- kBACKSPACE = #8;
- kMINUS = #45;
- kCOMMA = #44;
- kDOWN = 40;
- kUP = 38;
- kENTER = 13;
- kINSERT = 45;
- implementation
- uses MainUnit;
- {$R *.dfm}
- procedure ClearAddRecordForm;
- begin
- With AddRecordForm Do
- Begin
- HardwareNameEdit.Text := '';
- CompanyNameEdit.Text := '';
- DescriptionEdit.Text := '';
- WarrantyEdit.Text := '';
- PriceEdit.Text := '';
- End;
- end;
- Function CreateNewRecord: THardwareRecord;
- Var
- NewHardwareRecord: THardwareRecord;
- Begin
- With NewHardwareRecord, AddRecordForm Do
- Begin
- HardwareName := HardwareNameEdit.Text;
- CompanyName := CompanyNameEdit.Text;
- Description := DescriptionEdit.Text;
- Warranty := StrToInt(WarrantyEdit.Text);
- Price := Int(StrToFloat(PriceEdit.Text) * 100) / 100;
- End;
- CreateNewRecord := NewHardwareRecord;
- End;
- Procedure AddRecordToGrid(NewHardwareRecord: THardwareRecord);
- Begin
- With MainForm.StringGrid, NewHardwareRecord Do
- Begin
- if IsStringGridNotEmpty then
- RowCount := RowCount + 1
- else
- IsStringGridNotEmpty := True;
- Cells[0, RowCount-1] := HardwareName;
- Cells[1, RowCount-1] := CompanyName;
- Cells[2, RowCount-1] := Description;
- Cells[3, RowCount-1] := IntToStr(Warranty) + ' мес';
- Cells[4, RowCount-1] := FloatToStr(Price);
- Cells[DelColomn, RowCount-1] := ' X';
- Cells[EditColomn, RowCount-1] := ' ...';
- End;
- IsSomeChanges := True;
- End;
- procedure AddRecordToCorrectedRecordsFile(NewHardwareRecord: THardwareRecord);
- begin
- Seek(CorrectedRecordsFile, FileSize(CorrectedRecordsFile));
- Write(CorrectedRecordsFile, NewHardwareRecord);
- end;
- procedure TAddRecordForm.AddButtonClick(Sender: TObject);
- Var
- NewHardwareRecord: THardwareRecord;
- begin
- NewHardwareRecord := CreateNewRecord();
- AddRecordToGrid(NewHardwareRecord);
- AddRecordToCorrectedRecordsFile(NewHardwareRecord);
- AddRecordForm.Close;
- end;
- procedure TAddRecordForm.CancelButtonClick(Sender: TObject);
- begin
- AddRecordForm.Close;
- end;
- procedure TAddRecordForm.CopyButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CopyToClipboard;
- end;
- procedure TAddRecordForm.CopyPastePopupMenuPopup(Sender: TObject);
- var
- IValue: Integer;
- FlValue: Single;
- Buffer: String;
- begin
- Buffer := Clipboard.AsText;
- PasteButton.Enabled := True;
- if ActiveControl.Name = 'WarrantyEdit' then
- PasteButton.Enabled := TryStrToInt(Buffer, IValue)
- else
- if ActiveControl.Name = 'PriceEdit' then
- PasteButton.Enabled := TryStrToFloat(Buffer, FlValue);
- end;
- procedure TAddRecordForm.CutButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CutToClipboard;
- end;
- Function CheckPriceEdit():Boolean;
- var
- IsEditCorrect: Boolean;
- FlValue: Single;
- Begin
- with AddRecordForm.PriceEdit do
- begin
- IsEditCorrect := (Text <> '') And (TryStrToFloat(Text, FlValue) And (Text[1] <> ',') And (Text[High(Text)] <> ','));
- end;
- CheckPriceEdit := IsEditCorrect;
- End;
- Procedure EditAddButtonEnabled;
- var
- IsRecordCorrect: Boolean;
- Begin
- with AddRecordForm do
- begin
- IsRecordCorrect := (HardwareNameEdit.Text <> '') And (CompanyNameEdit.Text <> '') And (DescriptionEdit.Text <> '') And (WarrantyEdit.Text <> '') And CheckPriceEdit;
- AddRecordForm.AddButton.Enabled := IsRecordCorrect;
- end;
- End;
- procedure TAddRecordForm.StrEditChange(Sender: TObject);
- begin
- EditAddButtonEnabled;
- end;
- procedure TAddRecordForm.WarrantyEditChange(Sender: TObject);
- var
- CursPos: Byte;
- TempStr: String;
- IValue: Integer;
- begin
- with WarrantyEdit do
- begin
- CursPos := SelStart;
- TempStr := Text;
- if not TryStrToInt(TempStr, IValue) or (IValue < 0) then
- begin
- Delete (TempStr, SelStart, 1);
- Text := TempStr;
- SelStart := CursPos-1;
- end
- else
- begin
- Text := IntToStr(IValue);
- SelStart := CursPos;
- end;
- end;
- EditAddButtonEnabled;
- end;
- procedure TAddRecordForm.PriceEditChange(Sender: TObject);
- var
- TempStr: String;
- CursPos: Byte;
- begin
- with PriceEdit do
- begin
- TempStr := Text;
- CursPos := SelStart;
- if (length(TempStr)>1) And (TempStr[1] = '0') And (TempStr[2] = '0') then
- begin
- Delete(TempStr, 2, 1);
- Text := TempStr;
- SelStart := CursPos-1;
- end;
- end;
- EditAddButtonEnabled;
- end;
- procedure TAddRecordForm.EditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- Begin
- case key of
- kENTER, kDOWN:
- begin
- SelectNext(ActiveControl, True, True);
- with TEdit(ActiveControl) do
- SelStart := Length(Text);
- end;
- kUP:
- begin
- SelectNext(ActiveControl, False, True);
- with TEdit(ActiveControl) do
- SelStart := Length(Text);
- end;
- kINSERT:
- Key := 0;
- end;
- End;
- procedure TAddRecordForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- ClearAddRecordForm;
- ActiveControl := HardwareNameEdit;
- end;
- procedure TAddRecordForm.StrEditKeyPress(Sender: TObject;
- var Key: Char);
- begin
- if Key = #13 then
- Key := #0;
- end;
- procedure TAddRecordForm.EditDblClick(Sender: TObject);
- begin
- TEdit(Sender).Text := '';
- end;
- procedure TAddRecordForm.WarrantyEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if Not (Key in ['0'..'9', kBACKSPACE]) then
- Key := #0;
- end;
- procedure TAddRecordForm.PriceEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if Not (Key in ['0'..'9', kBACKSPACE, kCOMMA]) then
- Key := #0;
- end;
- procedure TAddRecordForm.PasteButtonClick(Sender: TObject);
- var
- CursPos: Byte;
- TempStr: String;
- ActiveEdit: TEdit;
- IValue: Integer;
- FlValue: Single;
- begin
- ActiveEdit := TEdit(ActiveControl);
- with ActiveEdit do
- begin
- CursPos := SelStart;
- TempStr := Text;
- PasteFromClipboard;
- if ActiveControl.Name = 'WarrantyEdit' then
- begin
- if not TryStrToInt(Text, IValue) or (IValue < 0) then
- begin
- Text := TempStr;
- SelStart := CursPos;
- end;
- end
- else
- if ActiveControl.Name = 'PriceEdit' then
- begin
- if not TryStrToFloat(Text, FlValue) or (FlValue < 0) then
- begin
- Text := TempStr;
- SelStart := CursPos;
- end
- end;
- End;
- end;
- end.
- unit EditRecordUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Clipbrd;
- type
- TEditRecordForm = class(TForm)
- HardwareNameLabel: TLabel;
- TitleLabel: TLabel;
- CompanyNameLabel: TLabel;
- DescriptionLabel: TLabel;
- WarrantyLabel: TLabel;
- PriceLabel: TLabel;
- HardwareNameEdit: TEdit;
- CompanyNameEdit: TEdit;
- DescriptionEdit: TEdit;
- WarrantyEdit: TEdit;
- EditButton: TButton;
- CancelButton: TButton;
- PriceEdit: TEdit;
- CopyPastePopupMenu: TPopupMenu;
- CopyButton: TMenuItem;
- PasteButton: TMenuItem;
- CutButton: TMenuItem;
- procedure CancelButtonClick(Sender: TObject);
- procedure EditButtonClick(Sender: TObject);
- procedure CopyPastePopupMenuPopup(Sender: TObject);
- procedure CopyButtonClick(Sender: TObject);
- procedure CutButtonClick(Sender: TObject);
- procedure PasteButtonClick(Sender: TObject);
- procedure StrEditChange(Sender: TObject);
- procedure WarrantyEditChange(Sender: TObject);
- procedure PriceEditChange(Sender: TObject);
- procedure EditKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure HardwareNameEditDblClick(Sender: TObject);
- procedure WarrantyEditKeyPress(Sender: TObject; var Key: Char);
- procedure PriceEditKeyPress(Sender: TObject; var Key: Char);
- procedure StrEditKeyPress(Sender: TObject; var Key: Char);
- procedure FormShow(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- EditRecordForm: TEditRecordForm;
- CurrRow: Integer;
- const
- kBACKSPACE = #8;
- kMINUS = #45;
- kCOMMA = #44;
- kDOWN = 40;
- kUP = 38;
- kENTER = 13;
- kINSERT = 45;
- implementation
- uses
- MainUnit;
- {$R *.dfm}
- procedure TEditRecordForm.CancelButtonClick(Sender: TObject);
- begin
- EditRecordForm.Close;
- end;
- Function CreateNewRecord: THardwareRecord;
- Var
- NewHardwareRecord: THardwareRecord;
- Begin
- With NewHardwareRecord, EditRecordForm Do
- Begin
- HardwareName := HardwareNameEdit.Text;
- CompanyName := CompanyNameEdit.Text;
- Description := DescriptionEdit.Text;
- Warranty := StrToInt(WarrantyEdit.Text);
- Price := Int(StrToFloat(PriceEdit.Text) * 100) / 100;
- End;
- CreateNewRecord := NewHardwareRecord;
- End;
- ////////////////////////////POPUP MENU /////////////////////////////////////
- procedure TEditRecordForm.CopyPastePopupMenuPopup(Sender: TObject);
- var
- IValue: Integer;
- FlValue: Single;
- Buffer: String;
- begin
- Buffer := Clipboard.AsText;
- PasteButton.Enabled := True;
- if ActiveControl.Name = 'WarrantyEdit' then
- PasteButton.Enabled := TryStrToInt(Buffer, IValue)
- else
- if ActiveControl.Name = 'PriceEdit' then
- PasteButton.Enabled := TryStrToFloat(Buffer, FlValue);
- end;
- Procedure ReplaceRecordInGrid(NewHardwareRecord: THardwareRecord);
- Begin
- With MainForm.StringGrid, NewHardwareRecord Do
- Begin
- Cells[0, CurrRow] := HardwareName;
- Cells[1, CurrRow] := CompanyName;
- Cells[2, CurrRow] := Description;
- Cells[3, CurrRow] := IntToStr(Warranty) + ' мес';
- Cells[4, CurrRow] := FloatToStr(Price);
- End;
- IsSomeChanges := True;
- End;
- procedure ReplaceRecordInCorrectedFile(NewHardwareRecord: THardwareRecord);
- var
- RecordsNum: Integer;
- begin
- RecordsNum := CurrRow-1;
- Seek(CorrectedRecordsFile, RecordsNum);
- Write(CorrectedRecordsFile, NewHardwareRecord);
- Seek(CorrectedRecordsFile, FileSize(CorrectedRecordsFile));
- end;
- procedure TEditRecordForm.EditButtonClick(Sender: TObject);
- Var
- NewHardwareRecord: THardwareRecord;
- begin
- NewHardwareRecord := CreateNewRecord();
- ReplaceRecordInGrid(NewHardwareRecord);
- ReplaceRecordInCorrectedFile(NewHardwareRecord);
- EditRecordForm.Close;
- end;
- procedure TEditRecordForm.PasteButtonClick(Sender: TObject);
- var
- CursPos: Byte;
- TempStr: String;
- ActiveEdit: TEdit;
- IValue: Integer;
- FlValue: Single;
- begin
- ActiveEdit := TEdit(ActiveControl);
- with ActiveEdit do
- begin
- CursPos := SelStart;
- TempStr := Text;
- PasteFromClipboard;
- if ActiveControl.Name = 'WarrantyEdit' then
- begin
- if not TryStrToInt(Text, IValue) or (IValue < 0) then
- begin
- Text := TempStr;
- SelStart := CursPos;
- end;
- end
- else
- if ActiveControl.Name = 'PriceEdit' then
- begin
- if not TryStrToFloat(Text, FlValue) or (FlValue < 0) then
- begin
- Text := TempStr;
- SelStart := CursPos;
- end
- end;
- End;
- end;
- procedure TEditRecordForm.CopyButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CopyToClipboard;
- end;
- procedure TEditRecordForm.CutButtonClick(Sender: TObject);
- begin
- TEdit(ActiveControl).CutToClipboard;
- end;
- Function CheckPriceEdit():Boolean;
- var
- IsEditCorrect: Boolean;
- FlValue: Single;
- Begin
- with EditRecordForm.PriceEdit do
- begin
- IsEditCorrect := (Text <> '') And (TryStrToFloat(Text, FlValue) And (Text[1] <> ',') And (Text[High(Text)] <> ','));
- end;
- CheckPriceEdit := IsEditCorrect;
- End;
- Procedure EditEditButtonEnabled;
- var
- IsRecordCorrect: Boolean;
- Begin
- with EditRecordForm do
- begin
- IsRecordCorrect := (HardwareNameEdit.Text <> '') And (CompanyNameEdit.Text <> '') And (DescriptionEdit.Text <> '') And (WarrantyEdit.Text <> '') And CheckPriceEdit;
- EditRecordForm.EditButton.Enabled := IsRecordCorrect;
- end;
- End;
- procedure TEditRecordForm.StrEditChange(Sender: TObject);
- begin
- EditEditButtonEnabled;
- end;
- procedure TEditRecordForm.WarrantyEditChange(Sender: TObject);
- var
- CursPos: Byte;
- TempStr: String;
- IValue: Integer;
- begin
- with WarrantyEdit do
- begin
- CursPos := SelStart;
- TempStr := Text;
- if not TryStrToInt(TempStr, IValue) or (IValue < 0) then
- begin
- Delete (TempStr, SelStart, 1);
- Text := TempStr;
- SelStart := CursPos-1;
- end
- else
- begin
- Text := IntToStr(IValue);
- SelStart := CursPos;
- end;
- end;
- EditEditButtonEnabled;
- end;
- procedure TEditRecordForm.PriceEditChange(Sender: TObject);
- var
- TempStr: String;
- CursPos: Byte;
- begin
- with PriceEdit do
- begin
- TempStr := Text;
- CursPos := SelStart;
- if (length(TempStr)>1) And (TempStr[1] = '0') And (TempStr[2] = '0') then
- begin
- Delete(TempStr, 2, 1);
- Text := TempStr;
- SelStart := CursPos-1;
- end;
- end;
- EditEditButtonEnabled;
- end;
- procedure TEditRecordForm.EditKeyDown(Sender: TObject;
- var Key: Word; Shift: TShiftState);
- Begin
- case key of
- kENTER, kDOWN:
- begin
- SelectNext(ActiveControl, True, True);
- with TEdit(ActiveControl) do
- SelStart := Length(Text);
- end;
- kUP:
- begin
- SelectNext(ActiveControl, False, True);
- with TEdit(ActiveControl) do
- SelStart := Length(Text);
- end;
- kINSERT:
- Key := 0;
- end;
- End;
- procedure TEditRecordForm.FormShow(Sender: TObject);
- var
- WarrantyStr: String;
- Row: Integer;
- begin
- Row := CurrRow;
- HardwareNameEdit.Text := MainForm.StringGrid.Cells[0, Row];
- CompanyNameEdit.Text := MainForm.StringGrid.Cells[1, Row];
- DescriptionEdit.Text := MainForm.StringGrid.Cells[2, Row];
- WarrantyStr := MainForm.StringGrid.Cells[3, Row];
- WarrantyEdit.Text := Copy(WarrantyStr, 1, Length(WarrantyStr) - 4);
- PriceEdit.Text := MainForm.StringGrid.Cells[4, Row];
- ActiveControl := CancelButton;
- end;
- procedure TEditRecordForm.HardwareNameEditDblClick(Sender: TObject);
- begin
- TEdit(Sender).Text := '';
- end;
- procedure TEditRecordForm.StrEditKeyPress(Sender: TObject;
- var Key: Char);
- begin
- if Key = #13 then
- Key := #0;
- end;
- procedure TEditRecordForm.WarrantyEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if Not (Key in ['0'..'9', kBACKSPACE]) then
- Key := #0;
- end;
- procedure TEditRecordForm.PriceEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if Not (Key in ['0'..'9', kBACKSPACE, kCOMMA]) then
- Key := #0;
- end;
- end.
- unit InformationUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;
- type
- TInformationForm = class(TForm)
- StringGrid: TStringGrid;
- TitleLabel: TLabel;
- BackButton: TButton;
- procedure FormCreate(Sender: TObject);
- procedure BackButtonClick(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- InformationForm: TInformationForm;
- implementation
- uses MainUnit;
- {$R *.dfm}
- type
- TArrOfRecord = Array Of THardwareRecord;
- procedure TInformationForm.BackButtonClick(Sender: TObject);
- begin
- InformationForm.Close;
- end;
- procedure TInformationForm.FormClose(Sender: TObject; var Action: TCloseAction);
- var
- I: Integer;
- begin
- with StringGrid do
- begin
- for I := 1 to RowCount-1 do
- begin
- Cells[0,I] := '';
- Cells[1,I] := '';
- end;
- end;
- end;
- procedure TInformationForm.FormCreate(Sender: TObject);
- begin
- With StringGrid Do
- Begin
- ColWidths[0] := 210;
- ColWidths[1] := 80;
- Cells[0,0] := 'Фирма изготовитель';
- Cells[1,0] := 'Цена (BYN)';
- End;
- end;
- procedure OutputRecordsArr(RecordsArr: TArrOfRecord);
- var
- I: Integer;
- begin
- with InformationForm.StringGrid do
- begin
- For I := 0 To High(RecordsArr) Do
- Begin
- Cells[0, I+1] := RecordsArr[I].CompanyName;
- Cells[1, I+1] := FloatToStr(RecordsArr[I].Price);
- End;
- end;
- end;
- function SortArrayOfRecords(RecordsArr: TArrOfRecord): TArrOfRecord;
- Var
- I, K, IMin: Integer;
- Buf: THardwareRecord;
- Begin
- For K := 0 To High(RecordsArr)-1 Do
- Begin
- IMin := K;
- For I := K + 1 To High(RecordsArr) Do
- Begin
- If RecordsArr[I].Price < RecordsArr[IMin].Price Then
- IMin := I;
- End;
- Buf := RecordsArr[IMin];
- RecordsArr[IMin] := RecordsArr[K];
- RecordsArr[K] := Buf;
- End;
- SortArrayOfRecords := RecordsArr;
- End;
- function CreateArrayOfRecords(RowCount: Integer): TArrOfRecord;
- var
- RecordsArr: TArrOfRecord;
- TempRecord: THardwareRecord;
- I: Integer;
- begin
- SetLength(RecordsArr, RowCount-1);
- Seek(CorrectedRecordsFile, 0);
- for I := 0 to High(RecordsArr) do
- begin
- Read(CorrectedRecordsFile, TempRecord);
- RecordsArr[I] := TempRecord;
- end;
- CreateArrayOfRecords := RecordsArr;
- end;
- procedure TInformationForm.FormShow(Sender: TObject);
- var
- RowCount: Integer;
- RecordsArr: TArrOfRecord;
- begin
- RowCount := MainForm.StringGrid.RowCount;
- InformationForm.StringGrid.RowCount := RowCount;
- if IsStringGridNotEmpty then
- begin
- RecordsArr := CreateArrayOfRecords(RowCount);
- RecordsArr := SortArrayOfRecords(RecordsArr);
- OutputRecordsArr(RecordsArr);
- end;
- end;
- end.
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Grids;
- type
- THardwareRecord = Record
- HardwareName: String[17];
- CompanyName: String[17];
- Description: String[40];
- Warranty: Word;
- Price: Real;
- end;
- TMainForm = class(TForm)
- TitelLabel: TLabel;
- MainMenu: TMainMenu;
- FileMenuItem: TMenuItem;
- ManualMenuItem: TMenuItem;
- AboutDeveloperMenuItem: TMenuItem;
- OpenMenuItem: TMenuItem;
- SaveMenuItem: TMenuItem;
- StringGrid: TStringGrid;
- AddRecordButton: TButton;
- BackButton: TButton;
- OpenDialog: TOpenDialog;
- SaveDialog: TSaveDialog;
- InfoButton: TButton;
- procedure FormCreate(Sender: TObject);
- procedure BackButtonClick(Sender: TObject);
- procedure AddRecordButtonClick(Sender: TObject);
- procedure StringGridClick(Sender: TObject);
- procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
- Rect: TRect; State: TGridDrawState);
- procedure StringGridKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure FormShow(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure OpenMenuItemClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure SaveMenuItemClick(Sender: TObject);
- procedure InfoButtonClick(Sender: TObject);
- procedure AboutDeveloperMenuItemClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- const
- DelColomn = 5;
- EditColomn = 6;
- clGrayCarbon = TColor($232323);
- kRIGHT = 39;
- var
- MainForm: TMainForm;
- IsStringGridNotEmpty, IsSomeChanges: Boolean;
- RecordsFile, CorrectedRecordsFile, SaveFile: File Of THardwareRecord;
- implementation
- uses
- AddRecordUnit, EditRecordUnit, InformationUnit;
- {$R *.dfm}
- Procedure ResetRectFocus;
- Var
- NewFocusRect: TGridRect;
- Begin
- NewFocusRect.Left := 0;
- NewFocusRect.Top := 1;
- NewFocusRect.Right := 0;
- NewFocusRect.Bottom := 1;
- MainForm.StringGrid.Selection := NewFocusRect;
- End;
- Procedure DeleteRecordFromGrid (Row: Integer);
- Var
- I: Integer;
- Begin
- With MainForm.StringGrid Do
- Begin
- if RowCount = 2 then
- Begin
- Cells[0, 1] := '';
- Cells[1, 1] := '';
- Cells[2, 1] := '';
- Cells[3, 1] := '';
- Cells[4, 1] := '';
- Cells[DelColomn, 1] := '';
- Cells[EditColomn, 1] := '';
- IsStringGridNotEmpty := False;
- End
- Else
- Begin
- For I := Row + 1 To RowCount - 1 Do
- Begin
- Cells[0, I-1] := Cells[0, I];
- Cells[1, I-1] := Cells[1, I];
- Cells[2, I-1] := Cells[2, I];
- Cells[3, I-1] := Cells[3, I];
- Cells[4, I-1] := Cells[4, I];
- End;
- RowCount := RowCount - 1;
- End;
- End;
- IsSomeChanges := True;
- End;
- procedure DeleteRecordFromCorrectedFile(RecordsNum: Integer);
- var
- I, RecordsCount: Integer;
- TempRecord: THardwareRecord;
- begin
- RecordsCount := FileSize(CorrectedRecordsFile);
- for I := RecordsNum to RecordsCount-1 do
- begin
- Seek(CorrectedRecordsFile, I);
- Read(CorrectedRecordsFile, TempRecord);
- Seek(CorrectedRecordsFile, I-1);
- Write(CorrectedRecordsFile,TempRecord);
- end;
- Seek(CorrectedRecordsFile, RecordsCount-1);
- Truncate(CorrectedRecordsFile);
- end;
- Procedure EditRecord(Row: Integer);
- Begin
- CurrRow := Row; //В модуле EditRecordUnit глобальная переменная со значением номера редактируемой строки
- EditRecordForm.ShowModal;
- End;
- procedure TMainForm.StringGridClick(Sender: TObject);
- var
- SelectedRectCol, SelectedRectRow: Integer;
- begin
- SelectedRectCol := StringGrid.Selection.Right;
- SelectedRectRow := StringGrid.Selection.Bottom;
- if (IsStringGridNotEmpty) then
- case SelectedRectCol of
- DelColomn:
- Begin
- If MessageBox(Handle, 'Вы действительно хотите удалить выбранную запись?', 'Вы уверены?', MB_YESNO Or MB_ICONQUESTION) = IDYES Then
- DeleteRecordFromGrid(SelectedRectRow);
- DeleteRecordFromCorrectedFile(SelectedRectRow);
- ResetRectFocus;
- End;
- EditColomn:
- Begin
- EditRecord(SelectedRectRow);
- ResetRectFocus;
- End;
- end;
- end;
- procedure TMainForm.AboutDeveloperMenuItemClick(Sender: TObject);
- begin
- MessageBox(Handle, 'Разработчик: Наривончик Александр Михайлович, гр. 351004', 'О разработчике', MB_OK Or MB_ICONINFORMATION);
- end;
- procedure TMainForm.AddRecordButtonClick(Sender: TObject);
- begin
- AddRecordForm.ShowModal;
- end;
- procedure TMainForm.BackButtonClick(Sender: TObject);
- begin
- MainForm.Close;
- end;
- Procedure AddRecordToGrid(NewHardwareRecord: THardwareRecord);
- Begin
- With MainForm.StringGrid, NewHardwareRecord Do
- Begin
- if IsStringGridNotEmpty then
- RowCount := RowCount + 1
- else
- IsStringGridNotEmpty := True;
- Cells[0, RowCount-1] := HardwareName;
- Cells[1, RowCount-1] := CompanyName;
- Cells[2, RowCount-1] := Description;
- Cells[3, RowCount-1] := IntToStr(Warranty) + ' мес';
- Cells[4, RowCount-1] := FloatToStr(Price);
- Cells[DelColomn, RowCount-1] := ' X';
- Cells[EditColomn, RowCount-1] := ' ...';
- End;
- End;
- Function CheckSaveFile: Boolean;
- var
- CanBeSaved, IsFileCorrect: Boolean;
- Path: String;
- begin
- if MainForm.SaveDialog.Execute then
- begin
- IsFileCorrect := True;
- Path := MainForm.SaveDialog.FileName;
- AssignFile(SaveFile, Path);
- try
- Rewrite(SaveFile);
- except
- IsFileCorrect := False;
- MessageBox(MainForm.Handle, 'Не удалось открыть файл!', 'Ошибка', MB_OK Or MB_ICONERROR);
- end;
- if IsFileCorrect then
- CloseFile(SaveFile);
- CanBeSaved := IsFileCorrect;
- end
- else
- CanBeSaved := False;
- CheckSaveFile := CanBeSaved;
- end;
- procedure SaveDataFile;
- var
- RecordsCount, I: Integer;
- TempRecord: THardwareRecord;
- begin
- Rewrite(SaveFile);
- RecordsCount := FileSize(CorrectedRecordsFile);
- I := 0;
- Seek(CorrectedRecordsFile, 0);
- while I < RecordsCount do
- begin
- Read(CorrectedRecordsFile, TempRecord);
- Write(SaveFile, TempRecord);
- Inc(I);
- end;
- CloseFile(SaveFile);
- IsSomeChanges := False;
- end;
- procedure TMainForm.SaveMenuItemClick(Sender: TObject);
- begin
- If CheckSaveFile then
- SaveDataFile;
- end;
- procedure ClearStringGrid;
- var
- I: Integer;
- begin
- for I := MainForm.StringGrid.RowCount-1 DownTo 1 do
- DeleteRecordFromGrid(I);
- IsStringGridNotEmpty := False;
- end;
- procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- CloseFile(CorrectedRecordsFile);
- Erase(CorrectedRecordsFile);
- ClearStringGrid;
- end;
- procedure FillCorrectedRecordsFile;
- var
- TempRecord: THardwareRecord;
- RecordsCount, I: Integer;
- begin
- Reset(RecordsFile);
- RecordsCount := FileSize(RecordsFile);
- I := 0;
- while I < RecordsCount do
- begin
- Read(RecordsFile, TempRecord);
- Write(CorrectedRecordsFile, TempRecord);
- Inc(I);
- end;
- CloseFile(RecordsFile);
- end;
- procedure DownloadRecordsFromFile;
- var
- I, RecordsCount: Integer;
- NewRecord: THardwareRecord;
- begin
- Reset(RecordsFile);
- RecordsCount := FileSize(RecordsFile);
- if RecordsCount = 0 then
- IsStringGridNotEmpty := False
- else
- begin
- I := 0;
- while I < RecordsCount do
- begin
- Read(RecordsFile, NewRecord);
- AddRecordToGrid(NewRecord);
- Inc(I);
- end;
- end;
- CloseFile(RecordsFile);
- end;
- function CheckFileIn(const Path: String): Boolean;
- var
- IsFileCorrect: Boolean;
- begin
- IsFileCorrect := True;
- AssignFile(RecordsFile, Path);
- try
- Reset(RecordsFile);
- except
- IsFileCorrect := False;
- MessageBox(MainForm.Handle, 'Не удалось открыть файл!', 'Ошибка', MB_OK Or MB_ICONERROR);
- end;
- if IsFileCorrect then
- CloseFile(RecordsFile);
- CheckFileIn := IsFileCorrect;
- end;
- procedure ResetProgram;
- begin
- ClearStringGrid;
- Seek(CorrectedRecordsFile, 0);
- Truncate(CorrectedRecordsFile);
- end;
- procedure TMainForm.OpenMenuItemClick(Sender: TObject);
- var
- Path: String;
- IsFileCorrect: Boolean;
- begin
- if OpenDialog.Execute then
- begin
- Path := OpenDialog.FileName;
- IsFileCorrect := CheckFileIn(Path);
- if IsFileCorrect then
- begin
- ResetProgram;
- DownloadRecordsFromFile;
- FillCorrectedRecordsFile;
- IsSomeChanges := False;
- end;
- end;
- end;
- procedure CreateEmptyCorrectedRecordsFile;
- begin
- AssignFile (CorrectedRecordsFile, 'CorrectedFile.hdb');
- Rewrite(CorrectedRecordsFile);
- end;
- procedure TMainForm.FormShow(Sender: TObject);
- begin
- CreateEmptyCorrectedRecordsFile;
- IsStringGridNotEmpty := False;
- IsSomeChanges := False;
- end;
- procedure TMainForm.InfoButtonClick(Sender: TObject);
- begin
- InformationForm.ShowModal;
- end;
- procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- if IsStringGridNotEmpty And IsSomeChanges then
- if MessageBox(Handle, 'Вы хотите сохранить файл данных?', 'Cохранить?', MB_YESNO Or MB_ICONQUESTION) = IDYES Then
- begin
- CanClose := CheckSaveFile;
- If CanClose then
- SaveDataFile;
- end;
- end;
- procedure TMainForm.FormCreate(Sender: TObject);
- begin
- With StringGrid Do
- Begin
- ColWidths[0] := 120;
- ColWidths[1] := 120;
- ColWidths[2] := 240;
- ColWidths[3] := 70;
- ColWidths[4] := 70;
- Cells[0,0] := 'Название устройства';
- Cells[1,0] := 'Фирма изготовитель';
- Cells[2,0] := 'Главная техническая характеристика';
- Cells[3,0] := 'Гарантия';
- Cells[4,0] := 'Цена (BYN)';
- End;
- end;
- procedure TMainForm.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
- Rect: TRect; State: TGridDrawState);
- begin
- if IsStringGridNotEmpty And (Arow > 0) then
- case Acol of
- DelColomn:
- With StringGrid Do
- Begin
- Canvas.Brush.Color := clGrayCarbon;
- Canvas.FillRect(CellRect(Acol, Arow));
- Canvas.TextOut(Rect.Left, Rect.Top, Cells[ACol, ARow]);
- End;
- EditColomn:
- With StringGrid Do
- Begin
- Canvas.Brush.Color := clGrayCarbon;
- Canvas.FillRect(CellRect(Acol, Arow));
- Canvas.TextOut(Rect.Left, Rect.Top, Cells[ACol, ARow]);
- End;
- end;
- end;
- procedure TMainForm.StringGridKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- var
- SelectedRectCol: Integer;
- begin
- SelectedRectCol := StringGrid.Selection.Right;
- If (Key = kRIGHT) And (SelectedRectCol = DelColomn-1) Then
- Key := 0;
- end;
- end.
- unit StartUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
- Vcl.Imaging.pngimage, Vcl.Menus, MainUnit;
- type
- TStartForm = class(TForm)
- ProgramNameLabel: TLabel;
- StartButtonLabel: TLabel;
- DogImage: TImage;
- StartMainMenu: TMainMenu;
- ManualMenuItem: TMenuItem;
- AboutDeveloperMenuItem: TMenuItem;
- ExitLabel: TLabel;
- procedure LabelMouseEnter(Sender: TObject);
- procedure LabelMouseLeave(Sender: TObject);
- procedure StartButtonLabelClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure ExitLabelClick(Sender: TObject);
- procedure AboutDeveloperMenuItemClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- StartForm: TStartForm;
- implementation
- {$R *.dfm}
- procedure TStartForm.StartButtonLabelClick(Sender: TObject);
- begin
- StartForm.Visible := False;
- MainForm.ShowModal;
- StartForm.Visible := True;
- end;
- procedure TStartForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Handle, 'Вы действительно хотите выйти?', 'Вы уверены?', MB_YESNO Or MB_ICONQUESTION) = IDYES;
- end;
- procedure TStartForm.AboutDeveloperMenuItemClick(Sender: TObject);
- begin
- MessageBox(Handle, 'Разработчик: Наривончик Александр Михайлович, гр. 351004', 'О разработчике', MB_OK Or MB_ICONINFORMATION);
- end;
- procedure TStartForm.ExitLabelClick(Sender: TObject);
- begin
- StartForm.Close;
- end;
- procedure TStartForm.LabelMouseEnter(Sender: TObject);
- begin
- With Sender As TLabel Do
- Begin
- Font.Size := Font.Size + 4;
- Top := Top - 5;
- Left := Left - 5;
- End;
- end;
- procedure TStartForm.LabelMouseLeave(Sender: TObject);
- begin
- With Sender As TLabel Do
- Begin
- Font.Size := Font.Size - 4;
- Top := Top + 5;
- Left := Left + 5;
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement