StringGrid 按回车直接到下一格,象EXCEL(100分)

  • 主题发起人 主题发起人 youngyu
  • 开始时间 开始时间
Y

youngyu

Unregistered / Unconfirmed
GUEST, unregistred user!
我在stringgrid中加入以下:
procedure TFormInput.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=13 then key:=9;
end;
但要按二次回车才能到下一格。能否象EXCEL一样按一次直接到下一格。
 
你把Options 中的goTags设成True
再加上以上代码就行了
我刚试过了!
 
把Options 中的goTags设成True
procedure TFormInput.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
postmessage(StringGrid1.Handle,WM_KEYDOWN,VK_TAB,0);
end;
 
这段代码我试了一下,肯定能解决你的问题

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = VK_Return then
keybd_event(VK_TAB, MapVirtualKey(VK_TAB, 0), 0, 0);
end;
 
procedure TForm1.StringGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=13 then stringgrid1.row:=stringgrid1.row+1;//下一行
// if key=13 then stringgrid1.col:=stringgrid1.col+1;//下一列
end;

如果在最后一行或最后一列不行的话,加上一下判断:
判断当前行是否是最后一行;当前列是否是最后一列。
 
if ord(key)=13 then
begin
if sgd.Col<> Sgd.ColCount-1 then
Sgd.col:=Sgd.Col+1
else
begin
if sgd.Row<>sgd.RowCount-1 then
begin
sgd.Row:=sgd.Row+1;
sgd.col:=1;
end;
 
把Options 中的goTags设成True
procedure TFormInput.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
postmessage(TStringGrid(Sender).Handle,WM_KEYDOWN,VK_TAB,0);
postmessage(TStringGrid(Sender).Handle,WM_KEYUP,VK_TAB,0);
end
end;
 
TO t365:
应是goTabs.试了,没有什么变化。第一次按回车如象是确认编辑(其实并没有做任何编辑),第二次回车移到下一格。
TO smallbs:
如有修改,可以了,如没有修改,按回车后跳到下下一格,多跳了一格。不行。
TO yc-zhj:
按回车直接跳出Stringgrid.
谢谢各位,还有没另的办法。
 
在没有修改的情况下,你怎么用回车的, 不会按了两次吧?
 
procedure TForm1.StringGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ord(key)=13 then stringGrid1.Row:=stringGrid1.Row+1;
end;
 
TO smallbs:
就是一进入STRINGGRID 就只按回车,跳格移动。
 
俺一直都是这样做的,你试试看。

if key=#13 then
if not (activecontrol is tstringgrid) then
begin
key:=#0;
perform(wm_nextdlgctl,0,0);
end else
if (activecontrol is tstringgrid) then
begin
with tstringgrid(activecontrol)do
if col+1<colcount then
col:=col+1
else col:=1;

end;
 
“ postmessage(StringGrid1.Handle,WM_KEYDOWN,VK_TAB,0) ” 它是不会跳两格的,你想想..
是不是与你写的其他代码有关,不妨单独开个工程试试看。.
 
谢谢大家.
 
多人接受答案了。
 
回车到下一个格子,到头自动到下一行开头!
if (Key = VK_RETURN) then
begin
if (StringGrid1.Col<>StringGrid1.ColCount-1)
then StringGrid1.col:=StringGrid1.Col+1
else
begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.Row:=StringGrid1.Row+1;
StringGrid1.col:=0;
end;
end;
 
后退
顶部