1.自己写了一个Edit,重载了keypress事件,当按会车时候,关闭edit的父窗体,窗体是SHOWMODAL的形式显示的。
2.我重载TForm的keydown,编译报错,说基类中找不到keydown,类似的还有ente,exit都无法重载,不解?

解决方案 »

  1.   

    可以啊:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;// 直接继承TEdit,另外再写一个控件也可以。
    type
      TEdit = class(StdCtrls.TEdit)
      protected
        procedure KeyPress(var Key: Char); override;
        procedure KeyDown(var Key: Word; Shift: TShiftState); override;
        procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
        procedure CMExit(var Message: TCMExit); message CM_EXIT;
      end;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TEdit.KeyPress(var Key: Char);
    var o: TWinControl;
    begin
      o := Self;
      if Key = #13 then begin
        while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
        (o.Parent as TForm).Close;
        Exit;
      end;
      inherited;
    end;procedure TEdit.KeyDown(var Key: Word; Shift: TShiftState);
    begin
      ShowMessage(chr(Key));
      inherited;
    end;procedure TEdit.CMEnter(var Message: TCMEnter);
    begin
      ShowMessage('Enter');
      inherited;
    end;procedure TEdit.CMExit(var Message: TCMExit);
    begin
      ShowMessage('Exit');
      inherited;
    end;end.
      

  2.   


        while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
        (o.Parent as TForm).Close;
        这段是什么意思,
       o := o.Parent?
      

  3.   

    循环找出Edit控件的父窗体,因为Edit控件有可能不是直接放在Form上的,可能放在其它控件如Panel上或者有更多层的关系。
      

  4.   

    但是你的循环已经找到了父窗体,为什么关闭的时候,还要(o.Parent as TForm).Close?
      

  5.   

    注意循环是在 o.Parent is TForm 时退出的,所以后面用o.Parent:
    while (o.Parent <> nil) and not (o.Parent is TForm) do o := o.Parent;
    (o.Parent as TForm).Close;或许这样更清晰,多了条语句:
    o := o.Parent;
    while (o <> nil) and not (o is TForm) do o := o.Parent;
    if o <> nil then  // 判断一下更保险
      (o as TForm).Close;
      

  6.   

    明白,再问一个简单问题我重载form的keydown,编译报错说,我的声明有问题,帮忙看一下procedure keydown(var key:word:shift:TShiftState);override;
    我认为没有问题呀?还有,为什么enter重载的时候,要加入message,而keydown不用
      

  7.   

    form的keydown也一样的啊,你声明时要放在protected或public下,不要在private下。OnEnter是由message直接调用的,你看看TWinControl的源码(Controls.pas),重载时只要把里面的照搬过去就行了。
      

  8.   

    dht2003(海)说得对,用分号:
    procedure keydown(var key:word; shift:TShiftState);override;
      

  9.   

    To Sysu(死树):
      前辈.小弟想请教一下.Delphi消息机制中WM_*代表Windows标准消息.那CM_、CN_打头的代表什么呀??比较晕。
      

  10.   

    procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;重写消息吧。