procedure TFrm_danju.StringGridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
beginwith stringgrid do
if key=13  then
  begin
  if col=colCount -1 then
     begin
       if row =rowcount-1 then row:=0; //如果这里row:=1;不会多行
       row :=row+1;
       col :=0 ;如果这里COL:=1;不会多列,不是我想要跳到第2列了
     end;
  col:=col+1;
//下面是我跟踪行,列,总行,总列几个值
  stringgrid.Cells[1,1]:=inttostr(stringgrid.row)+'行'+inttostr(stringgrid.col)+'列';
  stringgrid.Cells[2,2]:= inttostr(colcount);
  stringgrid.Cells[3,3]:= inttostr(rowcount);
  end;end;
//------------------------------------------------------------------------------
我有一个20行*9列的表格,上面的代码想实现的是从stringgrid.Cells[1,1]开始敲回车后到最后一行最后一列的时候,有回到stringgrid.Cells[1,1].不包含锁定列锁定行的第一行,第一列,
直接运行后,换行后多在锁定列后和锁定行下分别多一列,一行,并且复制了锁定列和锁定行的数据.
但多出来的这个行和列表格不能编辑.我不晓得问题出在那里了啊!请不吝赐教.谢谢!

解决方案 »

  1.   


    procedure TForm1.FormCreate(Sender: TObject);
    var
      i,j : integer;
    begin
      with StringGrid do
      begin
        RowCount := 21;
        ColCount := 10;
        for i := 1 to RowCount - 1 do
          for j := 1 to ColCount - 1 do
            cells[j,i] := '第' + inttostr(i) + '行 第' + inttostr(j) + '列';
      end;
    end;procedure TForm1.StringGridKeyPress(Sender: TObject; var Key: Char);
    begin
      if key = #13 then
      begin
        with StringGrid do
        begin
          if (Col = ColCount - 1) and (Row = RowCount - 1) then exit;
          if col <= ColCount - 2 then
            col := col + 1
          else
          begin
            col := 1;
            if Row <= RowCount - 2 then
              row := row + 1;
          end;
        end;
      end;
    end;
      

  2.   

    with StringGrid do
        begin
          if (Col = ColCount - 1) and (Row = RowCount - 1) then 
             //-------------------exit;
             //我把你的这里替换,实现我要的目的,就出现问题?
              begin   //我想在这个时候回到第一行第一列去;
                col:=1;
                row:=1;
              end;
            //---------------------一直不明白问题出在哪里了-----------------------------
          if col <= ColCount - 2 then
            col := col + 1
          else
          begin
            col := 1;
            if Row <= RowCount - 2 then
              row := row + 1;
          end;
        end;
      

  3.   

    //我把你的这里替换,实现我要的目的,就出现问题?
              begin   //我想在这个时候回到第一行第一列去;
                col:=1;
                row:=1;
    *******     exit     //这里要加这个,不然这时候COL=1,那么下面的代码还是执行的,
              end;
            //---------------------一直不明白问题出在哪里了-----------------------------
          if col <= ColCount - 2 then
            col := col + 1
          else
          begin
            col := 1;
            if Row <= RowCount - 2 then
              row := row + 1;
          end;