计数错误,一定是你的list只有2,你却访问了string[2]之类的造成的!一般strings是从0开始的。所以:
for i:=0 to listbox1.items.count-1 do
 .....

解决方案 »

  1.   

    另外你的Index可能为-1,引起错误
      

  2.   

    看看你的版本,我的delphi3是在编程书库里带的,就有这毛病,只好删了
      

  3.   

    各位大虾,我定义了一个二维数组box[1..14,1..16]
    程序中,for i:=1 to 14 do
         for j:=1 to 16 do后来根据大虾指点进行了若干次修改,还不行
    第一次修改:将二维数组定义为box[1..15,1..17]还报同样的错
    第二次修改:将二维数组定义为box[0..14,0..16],
               程序中, for i:=0 to 13 do
                          for J:=0 to 15 do
               还报同样的错
    第三次修改:将二维数组定义为box[0..15,0..17]
               程序中,for i:=1 to 14 do
                         for j:=1 to 16 do
               还报同样的错
    第四次修改:将二维数组定义为box[0..14,0..16]
               程序中,for i:=0 to 13 do
                        for j:=0 to 15 do
               还报同样的错
    第四次修改:将二维数组定义为box[0..14,0..16]
               程序中,for i:=1 to 13 do
                        for j:=1 to 15 do
               程序虽然不报错,但我需要的是14*16的数组,运行结果不对总之,i的取值只能是1..13,j的取值只能是1..15,可我需要14*16的数组!
    高手指点!
      

  4.   

    var  box:array [0..14,0..16] of ...;
    for I:=Low(box) to High(box) do  // 一维的下标到上标;
       for J:=Low(box[i]) to High(box[i]) do // 二维的下标到上标;
          .........
      

  5.   

    试试以下代码应该没有问题了:
    var box:array[0..13,0..15] of integer;
        i,j:integer;
    begin
      for i:=0 to 13 do
       for j:=0 to 15 do
        box[i,j]:=0;
    end;
      

  6.   

    还是不行,有劳各位看一下源程序,菜鸟比较笨,注释没怎麽写!请大虾见谅!
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, Menus, ComCtrls,inifiles,shellapi,mmsystem;type
      boxbase=array [0..13,0..15] of integer;
      TForm1 = class(TForm)
        black: TImage;
        boxok: TImage;
        diban: TImage;
        pushd1: TImage;
        pushd2: TImage;
        pushl1: TImage;
        pushl2: TImage;
        pushr1: TImage;
        pushr2: TImage;
        pushu2: TImage;
        qiang: TImage;
        rbox: TImage;
        ybox: TImage;
        MainMenu1: TMainMenu;
        Timer1: TTimer;
        StatusBar1: TStatusBar;
        N1: TMenuItem;
        N2: TMenuItem;
        N3: TMenuItem;
        N4: TMenuItem;
        N5: TMenuItem;
        N6: TMenuItem;
        N7: TMenuItem;
        N8: TMenuItem;
        N9: TMenuItem;
        N10: TMenuItem;
        pushu1: TImage;
        bkground: TImage;
        N11: TMenuItem;
        N12: TMenuItem;
        N13: TMenuItem;
        N14: TMenuItem;
        procedure FormCreate(Sender: TObject);
        procedure FormKeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure Timer1Timer(Sender: TObject);
       // procedure N4Click(Sender: TObject);
      //  procedure N5Click(Sender: TObject);
       // procedure N1Click(Sender: TObject);
       // procedure N9Click(Sender: TObject);
       // procedure N7Click(Sender: TObject);
      private
        { Private declarations }
        procedure drawimage(argx,argy,argimage:integer);
        function readfile:integer;
        procedure drawbkground;
      public
        { Public declarations }
        lastx,lasty:integer;
        localx,localy:integer;
        ifmove:boolean;
        comdir:string;
        filenum:integer;
        box:boxbase;
        boxnum:integer;
        boxini:tinifile;
        steps:integer;
        //soundflag:boolean;
    end;var
      Form1: TForm1;implementation
    {$R *.DFM}procedure tform1.drawbkground; //背景
    var i,j:integer;
    begin
    timer1.enabled:=false;
    for i:=0 to 13 do
      for j:=0 to 15 do
        drawimage(i,j,box[i][j]);
        timer1.enabled:=true;
        end; procedure tform1.drawimage(argx,argy,argimage:integer);
       var comimage:timage;
       begin
         case argimage of
         0:comimage:=form1.black;
         1:comimage:=form1.qiang;
         2:comimage:=form1.diban;
         3:comimage:=form1.ybox;
         4:comimage:=form1.boxok;
         7:comimage:=form1.rbox;
         51,61:comimage:=form1.pushu1;
         52,62:comimage:=form1.pushu2;
         53,63:comimage:=form1.pushl1;
         54,64:comimage:=form1.pushl2;
         55,65:comimage:=form1.pushr1;
         56,66:comimage:=form1.pushr2;
         57,67:comimage:=form1.pushd1;
         58,68:comimage:=form1.pushd2;
         else
           exit;
           end;
           form1.bkground.canvas.copyrect(rect((argy-1)*30,
               (argx-1)*30,argy*30,argx*30),
                    comimage.canvas,rect(0,0,30,30))
        end;    function tform1.readfile:integer;
        var filehandle:file of integer;
        i,j,fsize,funre:integer;
        filename:string;
        begin
        funre:=1;
        steps:=0;
        filename:=comdir+'\bbk\box'+inttostr(filenum)+'.bbk';
        if (not fileexists(filename)) then
           if filenum=1 then
           funre:=0
           else
           begin
           filenum:=1;
           filename:=comdir+'\bbk\box1.bbk';
           if not fileexists(filename) then
           funre:=0;
           end;
           if funre<>0 then
           begin
           boxnum:=0;
           assignfile(filehandle,filename);
           reset(filehandle);
           fsize:=filesize(filehandle);
           if fsize<>224 then
           funre:=0
           else
           begin
           form1.caption:='推箱子现在你推的是'+inttostr(filenum)+'关';
           for i:=0 to 13 do
           for j:=0 to 15 do
           begin
           read(filehandle,box[i][j]);
           if (box[i][j]=57) or (box[i][j]=58) then
               begin localx:=i;localy:=j;end;
           if (box[i][j]=3) then
           boxnum:=boxnum+1;
           end;
           end;
           closefile(filehandle);
           end
           else
           filenum:=0;
           readfile:=funre;
           end;
    procedure TForm1.FormCreate(Sender: TObject);
    var result,i,j:integer;
    begin
    timer1.enabled:=false;
    for i:=0 to 13 do
    for j:=0 to 15 do
    box[i][j]:=0;
    drawbkground;
    form1.width:=488;
    getdir(0,comdir);
    if comdir[strlen(pchar(comdir))]='\' then
       comdir[strlen(pchar(comdir))]:=' ';
    comdir:=trimright(comdir);
    lastx:=0;lasty:=0;
    localx:=0;localy:=0;
    boxini:=tinifile.create(comdir+'\box.ini');
    filenum:=boxini.readinteger('score','lastscore',0);
    //soundflag:=boxini.readbool('sound','sound',true);
    //n8.checked:=soundflag;
    result:=readfile;
    if result=0 then
    begin
       showmessage('没有可以过的关');
       filenum:=0;
       end
       else
       begin
       drawbkground;
       form1.caption:='推箱子 现在你推的是'+inttostr(filenum)+'关';
       timer1.enabled:=true;
       end;
       end;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var bestmove,xflag,yflag:integer;
    begin
       case key of
       37:begin
         xflag:=0;
         yflag:=-1;
         end;
       38:begin
          xflag:=-1;
          yflag:=0;
          end;
        39:begin
           xflag:=0;
           yflag:=1;
          end;
        40:begin
           xflag:=1;
           yflag:=0;
           end;
           else
           //if soundflag then
           //sndplaysound(pchar(comdir+'\sound\nomove.wav'),snd_async);
           exit;
          end;
           case box[localx+xflag][localy+yflag] of
           2:begin
           //if soundflag then
              //sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
              box[localx+xflag][localy+yflag]:=54+3*xflag+yflag;
              case box[localx][localy] of
                51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                end;
                ifmove:=false;
              end;
            4:begin
                  //if soundflag then
                     //sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                     box[localx+xflag][localy+yflag]:=64+3*xflag+yflag;
                     case box[localx][localy] of
                     51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                     61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                     end;
                  ifmove:=false;
               end;
            3:case box[localx+xflag+xflag][localy+yflag+yflag] of
                2:begin
                  //if soundflag then
                   // sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                    box[localx+xflag+xflag][localy+yflag+yflag]:=3;
                    box[localx+xflag][localy+yflag]:=54+3*xflag+yflag;
                     case box[localx][localy] of
                     51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                     61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                     end;
                     ifmove:=true;
                  end;
                4:begin
                    //if soundflag then
                    //sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                    box[localx+xflag+xflag][localy+yflag+yflag]:=7;
                    box[localx+xflag][localy+yflag]:=54+3*xflag+yflag;
                    case box[localx][localy] of
                     51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                     61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                    end;
                    ifmove:=true;
                  end;
                else
                     // begin
                     // if soundflag then
                     //sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                exit;
                     //end;
           end;
                     7:case box[localx+xflag+xflag][localy+yflag+yflag] of
                       2:begin
                    // if soundflag then
                    //sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                       box[localx+xflag+xflag][localy+yflag+yflag]:=3;
                       box[localx+xflag][localy+yflag]:=64+3*xflag+yflag;
                       boxnum:=boxnum+1;
                     case box[localx][localy] of
                     51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                     61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                     end;
                     ifmove:=true;
                     end;
                       4:begin
                  //if soundflag then
                   // sndplaysound(pchar(comdir+'\sound\move.wav'),snd_async);
                       box[localx+xflag+xflag][localy+yflag+yflag]:=7;
                       box[localx+xflag][localy+yflag]:=64+3*xflag+yflag;
                     case box[localx][localy] of
                     51,52,53,54,55,56,57,58:box[localx][localy]:=2;
                     61,62,63,64,65,66,67,68:box[localx][localy]:=4;
                     end;
                     ifmove:=true;
                      end;
                  else
                  //begin
                  //if soundflag then
                  //sndplaysound(pchar(comdir+'\sound\nomove.wav'),snd_async);
                  exit;
                  //end;
                  end;
                  else
                  //begin
                 // if soundflag then
                    // sndplaysound(pchar(comdir+'\sound\nomove.wav'),snd_async);
                     exit;
                     //end;
                     end;
                     lastx:=localx;
                     lasty:=localy;
                     localx:=localx+xflag;
                     localy:=localy+yflag;
                     timer1.enabled:=false;
                     steps:=steps+1;
                     statusbar1.panels[1].text:='你的移动步数为'+inttostr(steps);
                     drawbkground;
                     if (boxnum=0) then
                     begin
                     //if soundflag then
                       // sndplaysound(pchar(comdir+'\sound\over.wav'),snd_async);
                     bestmove:=boxini.readinteger('box','box'+inttostr(filenum),0);
                      if (steps<bestmove) or (bestmove=0) then
                      begin
                      boxini.writeinteger('box','box'+inttostr(filenum),steps);
                      showmessage('你做得太好了,我佩服你,多努力!');
                      end
                      else
                      showmessage('你还要多锻炼锻炼!只是时间问题!');
                      steps:=0;
                      statusbar1.panels[1].text:='你移动的步数为'+inttostr(steps);
                      filenum:=filenum+1;
                      lastx:=0;lasty:=0;
                      localx:=0;localy:=0;
                      readfile;
                      drawbkground;
                      end;
                      timer1.enabled:=true;
                      end;procedure TForm1.Timer1Timer(Sender: TObject);
    var  comimage:timage;
    begin
    timer1.enabled:=false;
    case box[localx][localy] of
    51,61:comimage:=pushr2;
    52,62:comimage:=pushr1;
    53,63:comimage:=pushl2;
    54,64:comimage:=pushl1;
    55,65:comimage:=pushu2;
    56,66:comimage:=pushu1;
    57,67:comimage:=pushd2;
    58,68:comimage:=pushd1;
    else
      exit;
    end;
    if (box[localx][localy] mod 2)=0 then
        box[localx][localy]:=box[localx][localy]-1
       else
         box[localx][localy]:=box[localx][localy]+1;
        bkground.canvas.copyrect(rect(localy*30-30,localx*30-30,localy*30,localx*30),
        comimage.canvas,rect(0,0,30,30));
        timer1.enabled:=true;
        end;{procedure TForm1.N4Click(Sender: TObject);
    var i,j:integer;
    begin
    //if soundflag then
     //  sndplaysound(pchar(comdir+'\sound\backbox.wav'),snd_async);
       steps:=steps-1;
       statusbar1.Panels[1].text:= '你的移动步数为'+inttostr(steps);
       case box[lastx][lasty] of
            2:box[lastx][lasty]:=57;
            4:box[lastx][lasty]:=67;
       end;
       if ifmove then
          case box[localx][localy] of
          51,52,53,54,55,56,57,58:box[localx][localy]:=3;
          61,62,63,64,65,66,67,68:box[localx][localy]:=7;
          end
          else
          case box[localx][localy] of
          51,52,53,54,55,56,57,58:box[localx][localy]:=2;
          61,62,63,64,65,66,67,68:box[localx][localy]:=4;
          end;
          if(lastx=localx) and (lasty=localy-1) then
          begin
            if ifmove then
               case box[localx][localy+1] of
               7:box[localx][localy+1]:=4;
               3:box[localx][localy+1]:=2;
               end;
               end;
               if (lastx=localx) and (lasty=localy+1) then
               begin
                 if ifmove then
                 begin
                   case box[localx][localy-1] of
                   7:box[localx][localy-1]:=4;
                   3:box[localx][localy-1]:=2;
                   end;
                   end;
                   if(lastx=localx+1) and (lasty=localy) then
                   begin
                     if ifmove then
                      case box[localx-1][localy] of
                   7:box[localx][localy-1]:=4;
                   3:box[localx][localy-1]:=2;
                   end;
               end;
     localx:=lastx;
     localy:=lasty;
     lastx:=0;
     lasty:=0;
     boxnum:=0;
     for i:=1 to 14 do
       for j:=1 to 16 do
        if (box[i][j]=3) then
         boxnum:=boxnum+1;
         drawbkground;
         end;          
         end;
    procedure tform1.N5Click(Sender: TObject);
    begin
      //if soundflag then
         //sndplaysound(pchar(comdir+'\sound\retry.wav'),SND_async);
         steps:=0;
         statusbar1.Panels[1].text:='你的移动步数为'+inttostr(steps);
         lastx:=0;lasty:=0;
         localx:=0;localy:=0;
         readfile;
         drawbkground;
         end;procedure TForm1.N1Click(Sender: TObject);
    begin
    //if soundflag then
    //sndplaysound(pchar(comdir+'\sound\click.wav'),snd_async);
    n4.enabled:=false;
    n5.enabled:=true;
    if lastx<>0 then
        n4.enabled:=true;
    end;procedure TForm1.N9Click(Sender: TObject);
    begin
    shellexecute(application.handle,pchar('open'),pchar('winhlp32.exe'),
    pchar('box.hlp'),pchar(comdir),sw_show);
    end;procedure TForm1.N7Click(Sender: TObject);
    begin
     boxini.writeinteger('score','lastscore',filenum);
     //boxini.writebool('sound','sound',soundflag);
     boxini.Destroy;
    end; }end.