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.
求解报错原因及解决方法
SetLength(d,c);
d长度不够,若刚好整除,则没问题,否则要+1
if (b mod 5)>0 then
SetLength(d,c+1)
else
SetLength(d,c);
后面循环最好不要用c,用这个:
for i:=low(d) to high(d) do