⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmain.pas

📁 Delphi7应用编程150例附书源码.rar
💻 PAS
字号:
unit UnitMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TStudentInfo = Record
   	Name: string[20];
    Sex: string[2];
  end;
type
  TfrmMain = class(TForm)
    AddButton: TButton;
    ModifyButton: TButton;
    DeleteButton: TButton;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure AddButtonClick(Sender: TObject);
    procedure ModifyButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  StudentFileType = file of TStudentInfo;
var
	StudentFile:StudentFileType;
	FileName:string[70];
var
  frmMain: TfrmMain;
implementation
uses UnitEdit;
{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
var
    i:Integer;
    StudentRec: TStudentInfo;
begin
    FileName:='Test.dat';
	  AssignFile(StudentFile,Filename);
    if FileExists(FileName) then
    begin
        Reset(StudentFile);
    end
    else
    begin
        ReWrite(StudentFile);
    end;

    StringGrid1.ColCount:=2;
    StringGrid1.RowCount:=FileSize(StudentFile);
    StringGrid1.FixedCols:=0;
    StringGrid1.FixedRows:=0;

    Seek(StudentFile,0);
    for i := 0 to FileSize(StudentFile)-1 do
    begin
       Read(StudentFile,StudentRec);
       StringGrid1.Cells[0,i]:=StudentRec.Name;
       StringGrid1.Cells[1,i]:=StudentRec.Sex;
    end;
end;

procedure TfrmMain.ModifyButtonClick(Sender: TObject);
var
	  StudentRec: TStudentInfo;
	  EditForm: Tfrm_Edit;
begin
	EditForm := Tfrm_Edit.Create(self);
	with EditForm do
	begin
	    EditForm.edt_Name.Text:= StringGrid1.Cells[0,StringGrid1.Row];
      EditForm.edt_Sex.Text:= StringGrid1.Cells[1,StringGrid1.Row];
	    if ShowModal <> idCancel then
	    begin
    	    StudentRec.Name := EditForm.edt_Name.Text;
          StudentRec.Sex:=EditForm.edt_Sex.Text;
          StringGrid1.Cells[0,StringGrid1.Row]:=StudentRec.Name;
          StringGrid1.Cells[1,StringGrid1.Row]:=StudentRec.Sex;
	        Seek(StudentFile,StringGrid1.Row);
	        Write(StudentFile,StudentRec);
	    end;
    end;
end;

procedure TfrmMain.DeleteButtonClick(Sender: TObject);
var
	  StudentRec: TStudentInfo;
	  i: Integer;
begin
    if MessageDlg('Delete Current Record ?', mtConfirmation,
	                [mbYes, mbNo], 0) = idYes then
    begin
        for i:=0 to StringGrid1.RowCount-1 do
        begin
            StringGrid1.Rows[i].Clear;
        end;

        Reset(StudentFile);
        for i:=0 to StringGrid1.Row-1 do
        begin
            Seek(StudentFile,i);
   	        Read(StudentFile,StudentRec);
	          Seek(StudentFile,i);
      	    Write(StudentFile,StudentRec);
        end;

	      for i := StringGrid1.Row+1 to StringGrid1.RowCount-1 do
    	  begin
	          Seek(StudentFile,i);
	          Read(StudentFile,StudentRec);
    	      Seek(StudentFile,i-1);
	          Write(StudentFile,StudentRec);
	      end;

	      Truncate(StudentFile);
        StringGrid1.RowCount:=FileSize(StudentFile);
        Seek(StudentFile,0);
        i:=0;
        While not Eof(StudentFile) do
        begin
            Read(StudentFile,StudentRec);
            StringGrid1.Cells[0,i]:=StudentRec.Name;
            StringGrid1.Cells[1,i]:=StudentRec.Sex;
            i:=i+1;
        end;
    end;
end;

procedure TfrmMain.AddButtonClick(Sender: TObject);
var
	  StudentRec: TStudentInfo;
	  EditForm: Tfrm_Edit;
begin
	  EditForm := Tfrm_Edit.Create(self);
	  if EditForm.ShowModal <> idCancel then
	  begin
	      StudentRec.Name := EditForm.edt_Name.Text;
        StudentRec.Sex:=EditForm.edt_Sex.Text;
  	    StringGrid1.RowCount:=FileSize(StudentFile)+1;
        StringGrid1.Cells[0,StringGrid1.RowCount-1]:=StudentRec.Name;
        StringGrid1.Cells[1,StringGrid1.RowCount-1]:=StudentRec.Sex;
	      Seek(StudentFile,FileSize(StudentFile));
	      Write(StudentFile,StudentRec);
	  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -