这段delphi代码怎么了,想不通啊

来源:百度知道 编辑:UC知道 时间:2024/05/15 01:24:50
var
Form1: TForm1;
idx:shortint;
newstr:string;
i:integer;
laststr:string;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
timer1.Interval :=10000;
timer1.Enabled :=true;

timer2.Interval :=200;
timer2.Enabled :=false;
end;
function TForm1.showwhattext(Num: shortint): widestring;
var//自定义要显示的不同内容
strlist:Tstringlist;
wstr:widestring ;
begin
case Num of
1:begin
strlist:=Tstringlist.Create ;
strlist.LoadFromFile(extractFilepath(application.ExeName)
+'textf\美国.txt');
wstr:=strlist.Text ;
result:=wstr;
end;
2:begin
strlist:=Tstringlist.Create ;
strlist.LoadFromFile(extractFilepath(application.ExeName)
+'textf\英国.txt');
wstr:=strlist.Text ;
res

在FormCreate中加入newstr:=showwhattext(idx);
出错的原因在于Timer2中newstr还没有任何东东,你却
inc(i);
laststr := laststr + newstr[i];//这边肯定出错
改进方法就是在timer2的代码执行前给newstr赋值

简单写了一下,基本满足功能需求,但楼主本人还得再在一些地方进行小的修改才行。经测试通过:
var
Form1: TForm1;
strlst: TStringList;
idx, txtIdx: integer;
str: string;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer2.Enabled := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
idx := 0;
txtIdx := 0;
label1.Font.Color :=clred;
label1.Font.Size :=16;
label1.Font.Name :='隶书';
label1.Caption := '';

strlst := TStringList.Create;
strlst.Add('您好!');
strlst.Add('是不是安装的卡巴2009啊?');
strlst.Add('请教一下:');
strlst.Add('师兄我在问个问题:代码如下');
strlst.Add('可是没有connectionstring属性');