请教:怎么在按键控制中,用Enter键代替Tab键。(10分)

  • 主题发起人 主题发起人 migis
  • 开始时间 开始时间
M

migis

Unregistered / Unconfirmed
GUEST, unregistred user!
我在数据库录入的,不想让Tab键切换下一个,而想换成Enter键切换。
 
Procedure TForm1.FormKeyPress(Sender:Tobject;Var Key:Char);
Begin
 if key=#13 then { 判断是按执行键}
 if not (ActiveControl is TDbgrid) Then
 Begin { 不是在TDbgrid控件内}
  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;
 
我在一个开发系统中用了如下代码,请你试试。
procedure TTaxAndCostStandardForm.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
if (ActiveControl is TDBGrid) then
begin
with DBGrid1 do
if SelectedIndex<(Columns.Count-1) then
begin
try
with DataModuleForm.Table_StandarRes do
if Modified then Post;
except
MessageBox(handle,PChar('源数据库文件错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;

SelectedIndex:=SelectedIndex+1;
end else begin
try
with DataModuleForm.Table_StandarRes do
begin
Next;
if eof then Append;
end;
except
MessageBox(handle,PChar('源数据库文件错误!'), szProgramTitle, MB_OK+MB_ICONWARNING);
end;
SelectedIndex:=0;
end;
end;
end;
 
yangjj为什么我录入了你的代码,可是显示如下:
[Error] Unit3.pas(117): Illegal character in input file: ' ' ($A1A1)
 
将Key=#13 改为Chr(Key)=VK_TAB试试.
 
[Error] Unit3.pas(117): Illegal character in input file: ' ' ($A1A1)
错误,你把前面的空格去掉就行了。或者拷我下面改过的就OK了
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then { 判断是按执行键}
if not (ActiveControl is TDbgrid) Then
Begin { 不是在TDbgrid控件内}
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;
 
怎么还是有错啊??
[Error] Unit3.pas(126): Undeclared identifier: 'SelectedIndex'
 
为什么没有人解答啊!!请帮帮忙啊!!!
 
将form1的keypreview属性设为true;
处理form1的onkeypress事件:
if key=#13 then SelectNext(activecontrol,true,true);
 
if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内}
begin
With TDbgrid(ActiveControl) Do
if Selectedindex<(FieldCount-1) then
Selectedindex:=Selectedindex+1{ 移动到下一字段}
else Selectedindex:=0;
end;

你把它删掉就不回出错了,不在DBGrid 里输入基本上要这段代码是没有用的。
 
Error] Unit3.pas(126): Undeclared identifier: 'SelectedIndex'
怎么会呢,你用DELPHI几?
还是把 下面这个删掉
else
if (ActiveControl is TDbgrid) Then{是在 TDbgrid 控件内}
begin
With TDbgrid(ActiveControl) Do
if Selectedindex<(FieldCount-1) then
Selectedindex:=Selectedindex+1{ 移动到下一字段}
else Selectedindex:=0;
end;
 
晚了:)
其实捕捉一下就可以了(If....Then):)
 
请说详细一点好吗?
 
onKeyPress 写
if key=#13 then
begin
postmessage(dbgrid1.handle,Wm_keydown,VK_tab,0);
key=#0;
end;
 
下载一个hubdog,看看不就得了
 
用ehlib's DBGrideh只要设置一个属性就可
 
都不用那么麻烦了,我有一个东东,只要往FORM上一放,所有的问题都搞定了!
 
将你的E-mail地址发至:hubin1224@sohu.com,我将给你最完美的答案,(含一个控件、原码、使用,for D5 D6 )
17:00我将查看你的邮件。

unit DosMove;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TMoveOptions = set of (moEnter,moUpDn);

TDosMove = class(TComponent)
private
FActive : boolean;
FOptions : TMoveOptions;
FEditNoBeep : boolean;
FOwnerKeyDown : TKeyEvent;
FOwnerKeyPress : TKeyPressEvent;
FLastWasEdit : boolean;
protected
procedure NewKeyDown(Sender : TObject;var Key : word;Shift : TShiftState);
procedure NewKeyPress(Sender : Tobject;var Key : char);
public
constructor Create(AOwner : TComponent); override;
published
property Active : boolean read FActive write FActive
default false;
property Options : TMoveOptions read FOptions write FOptions
default [moEnter,moUpDn];
property EditNoBeep : boolean read FEditNoBeep write FEditNoBeep
default true;
end;

procedure Register;

implementation

//-----------------------------------------------------------------------------
procedure Register;
begin
RegisterComponents('HomeMade', [TDosMove]);
end;

//-----------------------------------------------------------------------------
constructor TDosMove.Create(AOwner : TComponent);
var
Loop : integer;
begin
// First check to see no other TDosMove exists on the form
for Loop:=0 to AOwner.ComponentCount-1 do
if AOwner.Components[Loop] is TDosMove then raise
EInvalidOperation.Create('TDosMove can have only one instance per form');

// Create component and set default properties
inherited Create(AOwner);
FActive:=false;
FOptions:=[moEnter,moUpDn];
FEditNoBeep:=true;

// Intercept with OnKeyDown event and OnKeyPress event of 'Owner'
(AOwner as TForm).KeyPreview:=true;
FOwnerKeyDown:=(AOwner as TForm).OnKeyDown;
(AOwner as TForm).OnKeyDown:=NewKeyDown;
FOwnerKeyPress:=(AOwner as TForm).OnKeyPress;
(AOwner as TForm).OnKeyPress:=NewKeyPress;

end; // Create

//-----------------------------------------------------------------------------
procedure TDosMove.NewKeyDown(Sender : TObject;var Key : word;
Shift : TShiftState);
begin
if FActive then begin

// true if last active control is TCustomEdit and above
FLastWasEdit:=(Owner as TForm).ActiveControl is TCustomEdit;

if (FOptions<>[]) then begin

// Handle the specials keys
if ((Key=VK_DOWN) and (moUpDn in FOptions)) or
((Key=VK_RETURN) and (moEnter in FOptions)) then
(Owner as TForm).Perform(WM_NEXTDLGCTL,0,0)
else if (Key=VK_UP) and (moUpDn in FOptions) then
(Owner as TForm).Perform(WM_NEXTDLGCTL,1,0);
end; // if Options<>[] ...

end; // if FActive ...

// Call owner OnKeyDown if it's assigned
if assigned(FOwnerKeyDown) then FOwnerKeyDown(Sender,Key,Shift);
end; // NewKeyDown

//-----------------------------------------------------------------------------
procedure TDosMove.NewKeyPress(Sender : TObject;var Key : char);
begin
if FActive then begin
// Handle 'Enter' key that makes Edits beep
if FEditNoBeep and FLastWasEdit and (Key=#13) then Key:=#0;

end; // if FActive ...

// Call owner OnKeyPress if it's assigned
if assigned(FOwnerKeyPress) then FOwnerKeyPress(Sender,Key);
end; // NewKeyPress

end.
 
将form1的keypreview属性设为true;
处理form1的onkeypress事件:
if key=#13 then SelectNext(activecontrol,true,true);
 
jswqg谢谢了。
 
后退
顶部