用下面这段就可以了,不用以前的这么麻烦
procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=VK_LEFT then begin
    with stringgrid1 do
      if col > 0 then
        col := col - 1;
  end;
  if key=VK_RIGHT then begin
    with stringgrid1 do
      if col < colcount then
        col := col + 1;
  end;
end;

解决方案 »

  1.   

    To Mercedes()
    你没理解我的问题所在
    我的问题是。 比如一个CELLS的值是'abcdefg',
    光标是在d和e 中间,我如何知道光标的位置。因为我想实现一个
    功能是,比如当用户在对某一个CELLS中的值进行编辑,CELLS的值是'abcdefg',
    而光标是在d和e之间,如果用户使用左移键,那么在STRINGGRID的缺省情况下
    ,光标是从d和e之间移动到c和d 之间, 问题是在当光标已经处于a之前,如果用户再使用左移键,那么我希望光标是
    移动到前一个CELLS,也就是你说的COL + 1,
    我的问题就是怎么判断光标已经位于当前CELLS的值的最前端了,当用户使用左移键,
    然后我就可以通过COL + 1 实现了。??谢谢
    谢谢
      

  2.   

    那么请看这段程序吧
    procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      cc:TInplaceEdit;
    begin
      if stringgrid1.EditorMode then begin
        cc:=stringgrid1.controls[0] as TInplaceEdit;
        if (key=VK_LEFT) and (cc.SelStart=cc.sellength) then begin
          stringgrid1.EditorMode:=false;
          PostMessage(StringGrid1.Handle,WM_KEYDOWN,Key,0);
          PostMessage(StringGrid1.Handle,WM_KEYUP,Key,0);
        end
        else
        if (key=VK_RIGHT) and (cc.SelStart=length(cc.Text)) then begin
          stringgrid1.EditorMode:=false;
          PostMessage(StringGrid1.Handle,WM_KEYDOWN,Key,0);
          PostMessage(StringGrid1.Handle,WM_KEYUP,Key,0);
        end;
      end;
    end;
    这回我应该没有看错吧:)
      

  3.   

    To Mercedes()?
    果然是高手,分数送上了,谢谢你的帮助,