【发布时间】:2016-03-15 12:29:53
【问题描述】:
我正在创建一个程序,该程序在记录数组中移动并将这些学生记录保存到文件中。
但是我现在希望将数据(学生姓名、班级、年级)重新加载到数组中,然后将它们显示在另一个表单的列表框中。
我尝试了几种方法,但都没有成功。
这是编写文件的代码:
unit NewStudent;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, studentdata;
{ TFormNewStudent }
Type
TFormNewStudent = class(TForm)
Button1: TButton;
ButtonAddStudent: TButton;
Button3: TButton;
ComboBoxPredictedGrade: TComboBox;
EditClass: TEdit;
EditName: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ButtonAddStudentClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
Type
TPupil = Record
Name:String[30];
ClassGroup:String;
ComboBoxPredictedGrade:Integer;
end;
var
FormNewStudent: TFormNewStudent;
StudentRecArray : Array[1..30] of TPupil;
StudentNo:integer;
studentFile:TextFile;
implementation
{$R *.lfm}
{ TFormNewStudent }
procedure TFormNewStudent.Button1Click(Sender: TObject);
begin
FormStudentData.visible:=true;
FormNewStudent.visible:=false;
end;
procedure TFormNewStudent.Button3Click(Sender: TObject);
begin
FormStudentData.visible:=False;
FormNewStudent.visible:=True;
end;
procedure TFormNewStudent.ButtonAddStudentClick(Sender: TObject);
var
newStudent:string;
Begin
assignfile(studentFile,'G:\ExamGen\studentfile.txt');
StudentRecArray[StudentNo].Name:=EditName.text;
StudentRecArray[StudentNo].ClassGroup:=EditClass.text;
StudentRecArray[StudentNo].ComboBoxPredictedGrade:=ComboBoxPredictedGrade.ItemIndex;
append(studentFile);
newStudent:=(StudentRecArray[StudentNo].Name)+','+(StudentRecArray[StudentNo].ClassGroup)+','+(IntToStr(StudentRecArray[StudentNo].ComboBoxPredictedGrade));
writeln(studentFile,newStudent);
closefile(StudentFile);
StudentNo := StudentNo + 1;
end;
procedure TFormNewStudent.FormCreate(Sender: TObject);
begin
ComboBoxPredictedGrade.Items.Add('A');
ComboBoxPredictedGrade.Items.Add('B');
ComboBoxPredictedGrade.Items.Add('C');
ComboBoxPredictedGrade.Items.Add('D');
ComboBoxPredictedGrade.Items.Add('E');
ComboBoxPredictedGrade.Items.Add('U');
end;
end.
屏幕截图 1:StudentFile
截图 2:AddStudent Form
【问题讨论】:
-
请将学生文件中的内容添加为文本,而不是屏幕截图。它实际上并没有那么大,我们将无法从图像中复制/粘贴。
-
您尝试过的几种方法是什么? “没有成功”是什么意思?有错误吗?结果与您的预期有何不同?如果您想要“最佳方法”,则可能是使用数据库来存储此信息。
-
你的
ClassGroup: string永远不会工作,因为它没有定义的长度。您需要使用ShortString,就像使用Name: string[30];一样。话虽如此,一个没有明确问题陈述的问题(我尝试了几种方法都没有成功)是非常难以提出的,并且图像只能用于无法演示的事情作为文本。由于您的文件是文本,因此绝对没有理由将其放在图像中,并且您的表单图像完全没有意义和无关紧要。如果您希望我们提供帮助,请让我们尽可能轻松地提供帮助。 -
你用的是什么版本的Delphi?
标签: lazarus freepascal