快捷搜索:  汽车  科技

编程可运行文件需要什么(9.键盘鼠标和文件编程)

编程可运行文件需要什么(9.键盘鼠标和文件编程)示例主要组件及属性:表单窗体:procedure Read (F v1[ v2 ...vn]);5.2写入有类型文件调用 Write 过程可以将数据写入到文件中,格式如下:procedure Write(F v1 v2 ...vn);5.3有类型文件的基本操作返回文件指针的当前位置function FilePos(var F): Longint;返回文件的大小function FileSize(var F): Integer;移动文件指针的位置procedure Seek(var F; n: Longint);截去当前位置后的所有数据procedure Truncate(var F);5.4有类型文件编程示例示例:编写一个学生记录表的保存和读取程序。界面如下如:主窗体:

5.有类型文件编程

有类型文件时一种具有一定数据类型的文件,是由指定数据组成。读写过程中所操作的对象是一个指定类型的数据。有类型文件的声明格式:

Type <文件类型> = File of type;

其中,type 可以是各种数据类型。

有类型数据文件可以确定文件指针在文件中的位置,可以随机存取。

5.1读取有类型文件

有类型文件读写可以同时进行。读取有类型文件通过调用 Read 过程来进行操作,格式如下:

procedure Read (F v1[ v2 ...vn]);5.2写入有类型文件

调用 Write 过程可以将数据写入到文件中,格式如下:

procedure Write(F v1 v2 ...vn);5.3有类型文件的基本操作

  • 返回文件指针的当前位置

function FilePos(var F): Longint;

  • 返回文件的大小

function FileSize(var F): Integer;

  • 移动文件指针的位置

procedure Seek(var F; n: Longint);

  • 截去当前位置后的所有数据

procedure Truncate(var F);5.4有类型文件编程示例

示例:编写一个学生记录表的保存和读取程序。界面如下如:

主窗体:

编程可运行文件需要什么(9.键盘鼠标和文件编程)(1)

表单窗体:

编程可运行文件需要什么(9.键盘鼠标和文件编程)(2)

示例主要组件及属性:

组件

属性

说明

Panel1

Align

alTop

对齐到顶部,其上存放按钮

StringGrid1

Align

alClient

充满可显示区域

ColCount

7

7列

RowCount

1

刚打开窗体时只显示一行,标题行

Options/goRowSelect

True

可选中行

SaveDialog1

文件保存对话框

OpenDialog1

文件打开对话框

主窗体示例代码:

unit Unit1; interface uses Winapi.Windows Winapi.Messages System.SysUtils System.Variants System.Classes Vcl.Graphics Vcl.Controls Vcl.Forms Vcl.Dialogs Vcl.Grids Vcl.ExtCtrls Vcl.StdCtrls; type TForm1 = class(TForm) Panel1: TPanel; StringGrid1: TStringGrid; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; SaveDialog1: TSaveDialog; OpenDialog1: TOpenDialog; procedure Button3Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } procedure WriteStudent(RowIndex: integer); public { Public declarations } end; StudentRecord = record sn name sex: string[32]; chinese math english: double; end; var Form1: TForm1; // student 变量只用于与 Form2 交换数据 student: StudentRecord; F: file of StudentRecord; implementation {$R *.dfm} uses Unit2; procedure TForm1.WriteStudent(RowIndex: Integer); begin with StringGrid1 do begin Cells[0 RowIndex] := inttostr(RowIndex); Cells[1 RowIndex] := student.sn; Cells[2 RowIndex] := student.name; Cells[3 RowIndex] := student.sex; Cells[4 RowIndex] := floattostr(student.chinese); Cells[5 RowIndex] := floattostr(student.math); Cells[6 RowIndex] := floattostr(student.english); end; end; procedure TForm1.FormCreate(Sender: TObject); begin with StringGrid1 do begin Cells[0 0] := ''; Cells[1 0] := '学号'; Cells[2 0] := '姓名'; Cells[3 0] := '性别'; Cells[4 0] := '语文'; Cells[5 0] := '数学'; Cells[6 0] := '英语'; end; end; procedure TForm1.Button1Click(Sender: TObject); var FileName: String; RowIndex: integer; s: StudentRecord; begin if OpenDialog1.Execute then begin StringGrid1.RowCount := 1; FileName := OpenDialog1.FileName; AssignFile(F FileName); Reset(F); while not Eof(F) do begin Read(F s); RowIndex := StringGrid1.RowCount; StringGrid1.RowCount := StringGrid1.RowCount 1; with StringGrid1 do begin Cells[0 RowIndex] := inttostr(RowIndex); Cells[1 RowIndex] := s.sn; Cells[2 RowIndex] := s.name; Cells[3 RowIndex] := s.sex; Cells[4 RowIndex] := floattostr(s.chinese); Cells[5 RowIndex] := floattostr(s.math); Cells[6 RowIndex] := floattostr(s.english); end; end; CloseFile(F); end; end; procedure TForm1.Button2Click(Sender: TObject); var FileName: String; RowIndex: integer; s: StudentRecord; begin // 保存 if StringGrid1.RowCount < 2 then begin ShowMessage('表格中没有数据,不能保存!'); Exit; end; if SaveDialog1.Execute then begin FileName := SaveDialog1.FileName; AssignFile(F FileName); Rewrite(F); for RowIndex := 1 to StringGrid1.RowCount - 1 do begin with StringGrid1 do begin s.sn := Cells[1 RowIndex]; s.name := Cells[2 RowIndex]; s.sex := Cells[3 RowIndex]; s.chinese := strtofloat(Cells[4 RowIndex]); s.math := strtofloat(Cells[5 RowIndex]); s.english := strtofloat(Cells[6 RowIndex]); end; Write(F s); end; CloseFile(F); end; end; procedure TForm1.Button3Click(Sender: TObject); var RowIndex: integer; begin // 添加记录 with student do begin sn := ''; name := ''; sex := ''; chinese := 0; math := 0; english := 0; end; if Form2.ShowModal = mrOk then begin StringGrid1.RowCount := StringGrid1.RowCount 1; RowIndex := StringGrid1.RowCount - 1; WriteStudent(RowIndex); end; end; procedure TForm1.Button4Click(Sender: TObject); var RowIndex: integer; begin // 编辑 RowIndex := StringGrid1.Selection.Top; if RowIndex <> 0 then begin with StringGrid1 do begin student.sn := Cells[1 RowIndex]; student.name := Cells[2 RowIndex]; student.sex := Cells[3 RowIndex]; student.chinese := strtofloat(Cells[4 RowIndex]); student.math := strtofloat(Cells[5 RowIndex]); student.english := strtofloat(Cells[6 RowIndex]); end; if Form2.ShowModal = mrOk then begin WriteStudent(RowIndex); end; end else ShowMessage('请选择要编辑的记录!'); end; procedure TForm1.Button5Click(Sender: TObject); var RowIndex i: Integer; begin // 删除 if StringGrid1.RowCount < 2 then Exit; RowIndex := StringGrid1.Selection.Top; if RowIndex <> 0 then begin for i := RowIndex to StringGrid1.RowCount - 1 do begin StringGrid1.Rows[i] := StringGrid1.Rows[i 1]; end; end; StringGrid1.RowCount := StringGrid1.RowCount - 1; end; procedure TForm1.Button6Click(Sender: TObject); begin // 退出 Close; end; end.

表单窗体示例代码:

unit Unit2; interface uses Winapi.Windows Winapi.Messages System.SysUtils System.Variants System.Classes Vcl.Graphics Vcl.Controls Vcl.Forms Vcl.Dialogs Vcl.StdCtrls Vcl.Mask; type TForm2 = class(TForm) Label1: TLabel; SnEdit: TEdit; NameEdit: TEdit; Label2: TLabel; Label3: TLabel; SexBoyRadioButton: TRadioButton; SexGirlRadioButton: TRadioButton; Label4: TLabel; Label5: TLabel; Label6: TLabel; Button1: TButton; Button2: TButton; ChineseMaskEdit: TMaskEdit; MathMaskEdit: TMaskEdit; EnglishMaskEdit: TMaskEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormShow(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.dfm} uses Unit1; procedure TForm2.FormShow(Sender: TObject); begin with student do begin SnEdit.Text := student.sn; NameEdit.Text := student.name; if student.sex = '男' then SexBoyRadioButton.Checked := true else SexGirlRadioButton.Checked := false; ChineseMaskEdit.Text := floattostr(student.chinese); MathMaskEdit.Text := floattostr(student.math); EnglishMaskEdit.Text := floattostr(student.english); end; end; procedure TForm2.Button1Click(Sender: TObject); begin // 取消 ModalResult := mrCancel; end; procedure TForm2.Button2Click(Sender: TObject); begin // 确定 if SnEdit.Text = '' then ShowMessage('请输入学号!') else if NameEdit.Text = '' then ShowMessage('请输入姓名!') else if ChineseMaskEdit.Text = '' then ShowMessage('请输入语文成绩!') else if MathMaskEdit.Text = '' then ShowMessage('请输入数学成绩!') else if EnglishMaskEdit.Text = '' then ShowMessage('请输入英语成绩!') else begin Student.sn := SnEdit.Text; Student.name := NameEdit.Text; if SexBoyRadioButton.Checked then Student.sex := '男' else Student.sex := '女'; Student.chinese := strtofloat(ChineseMaskEdit.Text); Student.math := strtofloat(MathMaskEdit.Text); Student.english := strtofloat(EnglishMaskEdit.Text); end; ModalResult := mrOk; end; end.



猜您喜欢: