假设文本中问题和答案之间使用空格分开的,则把刚才回答的内容改动一下
for i:=1 to num do
begin
read(txt,array_question[i].question);
read(txt,array_question[i].answer1);
read(txt,array_question[i].answer2);
end;
改为
for i:=1 to num do
begin
read(txt,temp);
array_question[i].question:=copy(temp,1,pos(' ',temp));
temp:=stringreplace(temp,array_question[i].question,'',[]);
array_question[i].answer1:=copy(temp,1,pos(' ',temp));
temp:=stringreplace(temp,array_question[i].answer1,'',[]);
array_question[i].answer2:=temp;
readln(txt);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IF Opendialog1.Execute then
listbox1.Items.LoadFromFile(opendialog1.FileName);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
s,s1,s2,s3:string;
begin
// radiogroup1 0:是,1:否
s:=listbox1.Items.Strings[listbox1.itemindex];
s1:=copy(s,1,pos('|',s)-1);
s:=copy(s,pos('|',s)+1,length(s)-pos('|',s));
s2:=copy(s,1,pos('|',s)-1);
s:=copy(s,pos('|',s)+1,length(s)-pos('|',s));
s3:=s;
label1.Caption:=inttostr(listbox1.ItemIndex);
label2.Caption:=s1;
if s2='是' then
radiogroup1.ItemIndex:=0
else if s3='是' then
radiogroup1.ItemIndex:=1
else radiogroup1.ItemIndex:=-1;
窗体的全局变量
type
component = record
question:string;
answer1:string;
answer2:string;
end;
var
Form1: TForm1;
line:integer;
array_question:array of component;
procedure TForm1.FormCreate(Sender: TObject);
var
txt:textfile;
num:integer;
i:integer;
temp:string;
mystrings:TStrings;
begin
if not FileExists('c:\1.txt') then
begin
showmessage('文本文件不存在!');
exit;
end ;
AssignFile(txt,'c:\1.txt');
Reset(txt);
mystrings:= TStringList.Create;
mystrings.loadfromfile('c:\1.txt');
num:=mystrings.count;
setlength(array_question,num+1);
if num=0 then
begin
showmessage('没有试题!');
exit
end
else
begin
for i:=1 to num do
begin
read(txt,array_question[i].question);
read(txt,array_question[i].answer1);
read(txt,array_question[i].answer2);
end;
line:=1;
Label1.Caption:='第'+inttostr(line)+'行';
Label2.Caption:=array_question[line].question;
RadioButton1.Caption:=array_question[line].answer1;
RadioButton2.Caption:=array_question[line].answer2;
end;
end;
procedure TForm1.Button2Click(Sender: TObject); 提交按钮
begin
if line<high(array_question) then
begin
line:=line+1;
Label1.Caption:='第'+inttostr(line)+'行';
Label2.Caption:=array_question[line].question;
RadioButton1.Caption:=array_question[line].answer1;
RadioButton2.Caption:=array_question[line].answer2;
end
else
begin
showmessage('已经是最后一道题了');
end;
end;