如何在动态产生的Edit中响应Edit的KeyPress事件? 如何在动态产生的Edit中响应Edit的KeyPress事件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面说的很清楚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. 动态加事件Edit.keyPress := Edit1KeyPress;procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);beginShowmessage('');end; 只要把自己定义的处理过程赋值给对应的OnKeyPress就可以了 只要理解一点:事件 也是 属性。意味着 事件也可以run time 指定的。也就是楼上的: Edit.keyPress := Edit1KeyPress; 但我是直接写一个像InputBox的对话输入框,全部都是动态产生并指定它们的位置的然后在它的Edit里面我想控制只输入数字. 事先定义一个跟onkeypress一样的过程然后动态赋值 Edit.onkeyPress := '定义的过程名'; InputBox一般没办法干涉它要不研究一下使用消息,有点复杂要不自己定义一个跟InputBox一样的窗体,自己处理就可以了 怎么在控件的父类添加一个属性呢? 连锁系统中分部向总部传输数据,大家是怎么来保证主键不重复的? 搞不清的问题 怎样判断一个ActiveX控件是否已经注册? 如何通过程序取得本机的DNS地址? 学生问题,欢迎进入 在delphi中怎样判断一个字符串是整型的????? 添加记录问题? 如何在dephi程序中设置系统的操作系统的分辨率、字体等显示属性 一个很重要的问题!! 超级简单问题,在线等,马上给分 巨简单!Delphi中仅选择目录或文件路径的控件是哪个?
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.
Edit.keyPress := Edit1KeyPress;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
Showmessage('');
end;
意味着 事件也可以run time 指定的。
也就是楼上的:
Edit.keyPress := Edit1KeyPress;
然后在它的Edit里面我想控制只输入数字.
然后动态赋值
Edit.onkeyPress := '定义的过程名';
要不研究一下使用消息,有点复杂
要不自己定义一个跟InputBox一样的窗体,自己处理就可以了