简单的空件EDIT输入回车时,移到下一控件,(20分)

  • 主题发起人 主题发起人 jhb
  • 开始时间 开始时间
J

jhb

Unregistered / Unconfirmed
GUEST, unregistred user!
我编了一个简单的空件, EDIT输入回车时,模拟 TAB键,移到下一控件,

但 敲回车就是不能用,请问为什么??

unit Edit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TEdit1 = class(TEdit)
private
procedure KeyPress(var Key: Char);override;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation
procedure tedit1.KeyPress(var Key: Char);
begin
if key=#13 then
begin
key:=#0;
perform(cm_dialogkey,vk_tab,0);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
inherited;
end
else inherited;
end;

procedure Register;
begin
RegisterComponents('jhb', [TEdit1]);
end;

end.
 
TEdit是一个TWinControl, TWinControl有一个方法叫做SelectNexe作这件事:
procedure TWinControl.SelectNext(CurControl: TWinControl; GoForward, CheckTabStop: Boolean);
友谊该方法是Protected,故不能直接在TEdit中应用. 但是, 对于你从TEdit中继承
来的就没问题了.

TMyEdit=Class(TEdit)
protected
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
end;
implementation
procedure TMyEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
if key=VK_Return then SelectNext(Self, true, true);
inherited KeyUp(key, Shift);
end;
end.

对于普通放到Form上的, TEdit控件, 使用下个过程作为它的OnKeyUp事件即可(实际
上是把TWinControl的FindNextControl方法的有关部分移植过来了. 如果把上个过
程的inherited语句加到本过程最后, 可以代替上个过程):
procedure TForm1.EditKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
I, StartIndex: Integer;
List: TList;
CurCtl: TWinControl;
begin
List := TList.Create;
CurCtl:=TWinControl(Sender);
try
GetTabOrderList(List);
if List.Count > 0 then
begin
StartIndex := List.IndexOf(CurCtl);
if StartIndex = -1 then
StartIndex := 0;
repeat
I := StartIndex+1;
if i>=List.Count then i:=0;
CurCtl:=List;
if CurCtl.CanFocus and
CurCtl.TabStop and
(CurCtl.Parent = Self) then
begin
CurCtl.SetFocus;
break;
end;
until (I = StartIndex);
end;
finally
List.Free;
end;
end;
 
在Edit控件的OnKeyDown事件中这样写:
if key=VK_Return
then 下一个控件.SetFocus ;
 
perform(cm_dialogkey,vk_tab,0);
应该向edit的父窗体发送!
getparentform(edit).perform(cm_dialogkey,vk_tab,0)
 
我做了一个类似的控件,除了return,上下左右键都有不同表现(不是简单地移
到前一个或后一个哦),不光能跟踪TCustomEdit类的控件,对Button,combobox,
list,grid,都有效。如果你感兴趣,我可以给你源码(可能的话帮我改进一下)
 
Another_eYes : may You Send A Copy To Me?
<a href="zgsong@shtdu.edu.cn">zgsong@shtdu.edu.cn</a>
 
怎么想到编这么个控件的,其实只要把form的keypreview设置true,再
在onkeypress里面加上selectnext,then everything is OK!
 
To nickname:
每次编程时写几百行相同代码和用鼠标在form上放个控件哪个方便?
再说既然是做控件, 当然不只提供按return就selectnext这么简单的功能。
至少上下左右键都得考虑(不能简单地selectnext, 按向下键输入焦点跳到右面一个控件
总感觉不大正常吧),实际做起来你就会知道并不是您想象地那么容易,即使是return
键selectnext也要考虑很多(在memo输入时换行难道输入焦点就跳掉了?)
 
这不是有现成的吗?

unit tedit1;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
TEdit1 = class(TEdit)
private

protected

procedure KeyPress(var Key: Char); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;

public

published

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TEdit1]);
end;

procedure TEdit1.KeyPress(var Key: Char);
var
MYForm: TCustomForm;
begin

if Key = #13 then
begin
MYForm := GetParentForm( Self );
if not (MYForm = nil ) then
SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
Key := #0;
end;

if Key <> #0 then inherited KeyPress(Key);

end;

procedure TEdit1.KeyDown(var Key: Word; Shift: TShiftState);
var
MYForm: TCustomForm;
CtlDir: Word;
begin

if (Key = VK_UP) or (Key = VK_DOWN) then
begin
MYForm := GetParentForm( Self );
if Key = VK_UP then CtlDir := 1
else CtlDir :=0;
if not (MYForm = nil ) then
SendMessage(MYForm.Handle, WM_NEXTDLGCTL, CtlDir, 0);
end
else inherited KeyDown(Key, Shift);

end;



end.
 
首先感谢大家的支持,我是郑州的,我刚学WINDOWS编程近三个月(DELPHI3.0 C/S),
计算机接触较晚(97年初,是打游戏),全靠自学,98,10月上网,12月底找到了“大富翁”,这儿的老师太多了,妙不可言!!

我的底子太薄,可能会问的很可笑,但也请认真对待!望大家多帮忙!!(请不要用英语 )
EMAIL:jihongbo@public2.zz.ha.cn

我只是想练习一下编控件,编一个支持数字,日期,字符等格式自动转换,想支持回车
故提了这个问题,

刚想把一大堆问题贴上来(在下面写的),发现 yaojiaqing 的回答,好象都有,
等我回去再试试,


huizhang 你好:
SelectNext也不行,这是我的原程序码,装后试用,不行
请再帮忙看一下,我想知道为什么,谢了!
unit myedit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
tmyedit = class(TEdit)
private
protected
procedure KeyUp(var Key: Word; Shift: TShiftState); override;
// Tshiftstate类做什么?能否解释以下?谢了!!
public
published
end;
procedure Register;
implementation
//主要部分是直接从你那里拷过来的
procedure TMyEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
if key=VK_Return then SelectNext(Self, true, true);
inherited KeyUp(key, Shift);

end;
procedure Register;
begin
RegisterComponents('jhb', [tmyedit]);
end;
end.


Another_eYes 你好:
给你的信收到了吗?当然想要你的 控件和原码,还有信中提到的“毒”


 
什么信?
没收到呀
控件源码已发出
 
TShiftState indicates the state of the Alt, Ctrl, and Shift keys and the mouse buttons.

Unit

Classes

type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble);

Description

The TShiftState type is used by key-event and mouse-event handlers to determine the state of the Alt, Ctrl, and Shift keys and the state of the mouse buttons when the event occurred. It is a set of flags that indicate the following:

Value Meaning

ssShift The Shift key is held down.
ssAlt The Alt key is held down.
ssCtrl The Ctrl key is held down.
ssLeft The left mouse button is held down.
ssMiddle The middle mouse button is held down.
ssDouble The mouse was double-clicked.
 
Another_eYes 你好:又给你发了一封信收到了吗?
你的EMAIL:sleepless_eyes@iname.com没写错吧??
 
1. 还是没收到
2. 信箱没错
3. 给你的控件收到了吗?
 
诸位:
这个问题已解决,由于分数太少,不好分配,(不是我吝啬,问题太多)

见量!(下次一定多点儿)



 
Another_eYes:
控件收到谢了,
邪门,再法一次,我就不信!
 

Similar threads

后退
顶部