如何在动态产生的Edit中响应Edit的KeyPress事件?

解决方案 »

  1.   

    下面说的很清楚unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button: TButton;
        procedure ButtonClick(Sender: TObject);
        procedure MyMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
        procedure MyMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
        procedure MyMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
        procedure MyButtonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      Count: Integer = 0;
      CanMove: Boolean = false;
      StartX, StartY : Integer;implementation{$R *.dfm}procedure TForm1.ButtonClick(Sender: TObject);
    var
      MyLabel: TLabel;
      MyButton: TButton;
    begin
      Inc(Count);
      MyLabel:= TLabel.Create(self);
      MyButton:= TButton.Create(self);
      MyLabel.Parent:= Form1;
      MyButton.Parent:= Form1;
      MyLabel.Left:= (Count-1)* 50;
      MyButton.Left:= (Count-1)* 80;
      MyLabel.Top:= (Count-1)* 20;
      MyButton.Top:= (Count-1)* 20;
      MyLabel.Name:= 'Label'+IntToStr(Count);
      MyButton.Name:= 'Button'+IntToStr(Count);
      MyLabel.Caption:= 'Label'+IntToStr(Count);
      MyButton.Caption:= 'Button'+IntToStr(Count);
      MyButton.OnClick:= MyButtonClick;
      MyLabel.OnMouseDown:= MyMouseDown;
      MyLabel.OnMouseMove:= MyMouseMove;
      MyLabel.OnMouseUp:= MyMouseUp;
    end;procedure TForm1.MyButtonClick(Sender: TObject);
    begin
      showmessage(TButton(Sender).Name);
    end;procedure TForm1.MyMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      CanMove:= true;
      StartX:= x;
      StartY:= y;
    end;procedure TForm1.MyMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
    //
      if CanMove then
        begin
          TLabel(Sender).Left:= TLabel(Sender).Left+ x-StartX;
          TLabel(Sender).Top:= TLabel(Sender).Top+ y-StartY;
        end;
    end;procedure TForm1.MyMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
    //
    CanMove:= false;
    end;end.
      

  2.   

    动态加事件
    Edit.keyPress := Edit1KeyPress;
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    Showmessage('');
    end;
      

  3.   

    只要把自己定义的处理过程赋值给对应的OnKeyPress就可以了
      

  4.   

    只要理解一点:事件 也是 属性。
    意味着 事件也可以run time 指定的。
    也就是楼上的:
      Edit.keyPress := Edit1KeyPress;
      

  5.   

    但我是直接写一个像InputBox的对话输入框,全部都是动态产生并指定它们的位置的
    然后在它的Edit里面我想控制只输入数字.
      

  6.   

    事先定义一个跟onkeypress一样的过程
    然后动态赋值 
    Edit.onkeyPress := '定义的过程名';
      

  7.   

    InputBox一般没办法干涉它
    要不研究一下使用消息,有点复杂
    要不自己定义一个跟InputBox一样的窗体,自己处理就可以了