procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); begin if key=#13 then {判断是按执行键} if not (ActiveControl is TDbgrid) Then { 不是在TDbgrid控件内} Begin key:=#0; perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件} end else if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内} begin With TDbgrid(ActiveControl) Do if Selectedindex<(FieldCount-1) then Selectedindex:=Selectedindex+1{ 移动到下一字段} else Selectedindex:=0; end; end;
記住設置form1的keypreview:=true;procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); begin if key=#13 then {判断是按执行键} if not (ActiveControl is TDbgrid) Then { 不是在TDbgrid控件内} Begin key:=#0; perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件} end else if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内} begin With TDbgrid(ActiveControl) Do if Selectedindex<(FieldCount-1) then Selectedindex:=Selectedindex+1{ 移动到下一字段} else Selectedindex:=0; end; end;
begin
if key=#13 then {判断是按执行键}
if not (ActiveControl is TDbgrid) Then { 不是在TDbgrid控件内}
Begin
key:=#0;
perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件}
end
else if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内}
begin
With TDbgrid(ActiveControl) Do
if Selectedindex<(FieldCount-1) then
Selectedindex:=Selectedindex+1{ 移动到下一字段}
else Selectedindex:=0;
end;
end;
dbgrid是从TCustomGrid继承下来的,它有col与row属性,只不过是protected的,不能直接访问,要处理一下,可以这样:TDrawGrid(dbgrid1).row:=row;TDrawGrid(dbgrid1).col:=col;dbgrid1.setfocus;就可以看到效果了。1 这个方法是绝对有问题的,它会引起DBGrid内部的混乱,因为DBGrid无法定位当前纪录,如果DBGrid只读也就罢了(只读还是会出向一些问题,比如原本只能单选的纪录现在可以出现多选等等,你可以自己去试试),如果DBGrid可编辑那问题就可大了,因为当前纪录的关系,你更改的数据字段很可能不是你想象中的2 我常用的解决办法是将上程序改为(随便设置col是安全的,没有一点问题)Query1.first;TDrawGrid(dbgrid1).col:=1;dbgrid1.setfocus;这就让焦点移到第一行第一列当中
begin
if key=#13 then {判断是按执行键}
if not (ActiveControl is TDbgrid) Then { 不是在TDbgrid控件内}
Begin
key:=#0;
perform(WM_NEXTDLGCTL,0,0);{移动到下一个控件}
end
else if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内}
begin
With TDbgrid(ActiveControl) Do
if Selectedindex<(FieldCount-1) then
Selectedindex:=Selectedindex+1{ 移动到下一字段}
else Selectedindex:=0;
end;
end;