如何在TEdit控件输入框中输入制表符?

小笨同学 2012-08-03 09:18:35
问题描述:窗体上有一个TEdit控件,我想在用户按下[TAB]键时输入一个制表符,而不是将焦点移到下一个控件?尝试过在OnKeyPress 和 OnKeyDown 中捕获[TAB]键,无果,求指示。
...全文
173 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小笨同学 2012-08-03
  • 打赏
  • 举报
回复
受益颇多,谢谢了
s11ss 2012-08-03
  • 打赏
  • 举报
回复
当然还可以由TEdit来处理以下消息的其中一种:
CM_WANTSPECIALKEY
WM_GETDLGCODE

思路源于Controls单元(Delphi 2007):
procedure TWinControl.CNKeyDown(var Message: TWMKeyDown);
var
Mask: Integer;
begin
with Message do
begin
Result := 1;
UpdateUIState(Message.CharCode);
if IsMenuKey(Message) then Exit;
if not (csDesigning in ComponentState) then
begin
if Perform(CM_CHILDKEY, CharCode, Integer(Self)) <> 0 then Exit;
Mask := 0;
case CharCode of
VK_TAB:
Mask := DLGC_WANTTAB;
VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:
Mask := DLGC_WANTARROWS;
VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:
Mask := DLGC_WANTALLKEYS;
end;
if (Mask <> 0) and
(Perform(CM_WANTSPECIALKEY, CharCode, 0) = 0) and
(Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and
(GetParentForm(Self).Perform(CM_DIALOGKEY,
CharCode, KeyData) <> 0) then Exit;
end;
Result := 0;
end;
end;
s11ss 2012-08-03
  • 打赏
  • 举报
回复
还可以由窗体处理CM_DIALOGKEY消息。
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
private
{ Private declarations }
procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.CMDialogKey(var Message: TCMDialogKey);
begin
if GetKeyState(VK_MENU) >= 0 then
with Message do
case CharCode of
VK_TAB:
if GetKeyState(VK_CONTROL) >= 0 then
begin
if ActiveControl is TEdit then
begin
(ActiveControl as TEdit).SelText := #9;
Result := 1;
Exit;
end;
end;
end;
inherited;
end;

end.
s11ss 2012-08-03
  • 打赏
  • 举报
回复
在窗体上拖一个TEdit控件,然后拷贝以下代码:
unit Unit1;

interface

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

type
TEdit = class(StdCtrls.TEdit)
procedure CNKeyDown(var Message: TWMKeyDown); message CN_KEYDOWN;
end;

TForm1 = class(TForm)
Edit1: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TEdit }

procedure TEdit.CNKeyDown(var Message: TWMKeyDown);
begin
if Message.CharCode = VK_TAB then
Self.SelText := #9
else
inherited;
end;

end.

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧