zoukankan      html  css  js  c++  java
  • delphi7 怎样实现Enter当作Tab

    介绍了Delphi 7下回车键当作TAB的三种方法,有代码可以直接复制使用

    delphi7 怎样写可以让 Enter=Tab
    2011-9-28 14:36
    提问者:ppwoshisong | 浏览次数:36次
    2011-9-28 15:09
    最佳答案

    正好我也用到了这方面...就转给你看看吧..

    方法一、先将窗体的KeyPreview属性设为True。然后在FormKeyPress事件中加入以下代码:
    procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
    begin
    if Key = #13 then { 如果按下了回车键 }
    begin
    Key := #0; { 吃掉回车键 }
    Perform(WM_NEXTDLGCTL, 0, 0); { 移动到下一个控制 }
    end;
    end;
    这个方法对于大多数控件都适用,但不适用于TButton控件,因为对于按钮,回车键等于按下了按钮,不触发FormKeyPress事件。

    要改写成一个自定义过程,可以这样:
    procedure MyKeyPress(frm:TForm; var Key: Char);
    begin
    if Key = #13 then { 如果按下了回车键 }
    begin
    Key := #0; { 吃掉回车键 }
    frm.Perform(WM_NEXTDLGCTL, 0, 0); { 移动到下一个控制 }
    end;
    end;

    procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
    begin
    MyKeyPress(Self, Key);
    end;

    如果你经常使用这样的窗体,你也可以在设置好KeyPreview属性和输入代码后,在Form上击右键,选择Add to Repository,这样把Form添加到Delphi的Repository库中,以后建立新窗口时,可以从Delphi的对话框的Forms中选择你添加的这个窗体作为模板。

    这的确是个问题。可以这样解决:在KeyPress事件中先判断一下控件的类型,如果当前控件是TMemo或TDBMemo(甚至可以进一步检查控件的名字中是否带有Memo的),就不进行移动,对于这样的控件还是通过Tab键移动。下面是代码:
    var sClassName: string;
    begin
    if Key = #13 then { 如果按下了回车键 }
    begin
    sClassName := TForm(Sender).ActiveControl.ClassName;
    if (sClassName <> 'TDBMemo')
    And (sClassName <> 'TMemo') then
    begin
    Key := #0; { 吃掉回车键 }
    Perform(WM_NEXTDLGCTL, 0, 0); { 移动到下一个控制 }
    end;
    end;
    方法二、

    在你的数据模块中,添加如下代码:

    interface

    。。。。。。

    type

    TMessageHandler = class //使得回车消息转换成Tab消息

    class procedure AppMessage(var Msg:TMsg;var Handled:Boolean);

    end;

    implementation



    class procedure TMessageHandler.AppMessage(var Msg: TMsg; var Handled: Boolean);

    begin

    if Msg.message=WM_KEYDOWN then

    if (Msg.wParam=VK_RETURN ) and

    (

    (Screen.ActiveForm.ActiveControl is TEdit) or

    (Screen.ActiveForm.ActiveControl is TComboBox) or




    (Screen.ActiveForm.ActiveControl is TCheckBox) or

    (Screen.ActiveForm.ActiveControl is TRadioButton)

    //可以添加需要的控件

    )

    then

    begin

     

    第1页

    下载原格式 txt 文档(共 2 页)
    微信  支付宝

    介绍了Delphi 7下回车键当作TAB的三种方法,有代码可以直接复制使用


    Msg.wParam:=VK_TAB;

    end

    else if (Msg.wParam=VK_RETURN) and

    (

    (Screen.ActiveForm.ActiveControl is TDBGrid)

    )

    then

    begin

    with Screen.ActiveForm.ActiveControl do

    begin

    if Selectedindex<(FieldCount-1) then

    Selectedindex:=Selectedindex+1{ 移动到下一字段}

    else

    Selectedindex:=0;

    end;

    end;

    end;

    为了使得整个应用程序都能够实现主要的功能,在主窗体的OnCreate事件中添加如下代码:

    procedure TfmMain.FormCreate(Sender: TObject);

    begin

    Application.OnMessage:=TMessageHandler.AppMessage;

    end;

    到此为止,你的应用程序已经实现了这个Enter->Tab的转换.

    方法三、

    Procedure TForm1.Edit1KeyPress(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 TDbg

    rid(ActiveControl) Do
    if Selectedindex<(FieldCount-1) then
    Selectedindex:=Selectedindex+1{ 移动到下一字段}
    else Selectedindex:=0;
    end;
    End;
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    整数的唯一分解定理【模板】
    poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】
    poj 1065 Wooden Sticks 【贪心 新思维】
    poj 3126 Prime Path 【bfs】
    hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】
    poj 1840 Eqs 【解五元方程+分治+枚举打表+二分查找所有key 】
    poj 1019 Number Sequence 【组合数学+数字x的位宽函数】
    括号匹配
    Digit
    Ace of Aces
  • 原文地址:https://www.cnblogs.com/jijm123/p/13961739.html
Copyright © 2011-2022 走看看