delphi数组循环报错
procedure TForm1.Button1Click(Sender: TObject);
var
d:array of string;
i:integer;
begin
a:=Form1.Label1.Caption;
Form1.Label1.Caption:='';
b:=length(a);
c:= b div 5 ;
SetLength(d,c);
for i:=0 to c do
begin
arr[i]:=Copy(a,5*(i)+1,5);
Form1.Label1.Caption:=Form1.Label1.Caption+arr[i]+#13#10;
end;
end.
程序代码如上 用以实现label1的长度5自动换行功能
编译可以通过 但是运行的时候 弹出提示
Project Project.exe raised exception class EAccessViolation with message `Access violation at address 00403E92 in module `Project1.exe Read of address 0000000A. Process stopped. Use Step or Run to continue.
求解报错原因及解决方法
[解决办法]
看你写的挺繁琐的,这样写吧
- Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);var a: string; i, len: integer;begin a := Label1.Caption; i := 6; len := Length(a); while i <= len do begin Insert(#13#10, a, i); Inc(i, 7); len := Length(a); end; Label1.Caption := a;end;
[解决办法]
arr 应该就是 d 吧,而且d 最后没有释放内存
楼主的程序写得有点奇怪,也许和我的习惯不同吧
[解决办法]
单从逻辑上看有问题,
应该是
for i:=0 to c-1
[解决办法]
只是为了让lable自动换行的话,有必要写这么多代码么,直接设WordWrap属性为True不就行了
[解决办法]
c:= b div 5
这句代码,正好整除时比不能整除时数组的长度会少1
所以下面的代码在正好整除时就会报错
for i:=0 to c do
[解决办法]
if (b mod 5)>0 then
SetLength(d,c+1)
else
SetLength(d,c);
后面循环最好不要用c,用这个:
for i:=low(d) to high(d) do