1. 控件TMyEdit 繼承TEdit,請寫出定義語句

2. 修改TMyEdit (繼承於TEdit),使其只接受數字輸入
用DELPHI 实现 

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        procedure Edit1KeyPress(Sender: TObject; var Key: Char);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if not (Key in['0'..'9',#8]) then Key:=#0;
    end;end.
      

  2.   


    unit untMyEdit;interfaceuses
      Classes, Windows, StdCtrls;type
      TMyEdit = class(TEdit)
      protected
        procedure KeyPress(var Key: Char); override;
        procedure KeyDown(var Key: Word; Shift: TShiftState); override;
      public
        constructor Create(AOwner: TComponent); override;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TMyEdit]);
    end;procedure TMyEdit.KeyPress(var Key: Char);
    begin
       if not (Key in ['0'..'9', Chr(VK_BACK), Chr(VK_RETURN), Chr(VK_LEFT), Chr(VK_RIGHT)]) then Key := #0
       else
        inherited KeyPress(KEY);
    end;procedure TMyEdit.KeyDown(var Key: Word; Shift: TShiftState);
    begin
      if not (Key in [Ord('0')..Ord('9'), VK_BACK, VK_RETURN, VK_LEFT, VK_RIGHT]) then Key := 0
       else
       inherited KeyDown(Key, Shift);
    end;constructor TMyEdit.Create(AOwner: TComponent);
    begin
      inherited;
      Text := '0';
    end;end.