为动态控件添加事件 delphi
//该程序是在动态窗口中添加动态按钮,并指定按钮事件。完整测试代码如下:programProject1;usesWindows,Messages,SysUtils,Var...
//该程序是在动态窗口中添加动态按钮,并指定按钮事件。完整测试代码如下:
program Project1;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
var
MyButton1:TButton;
MyForm1:TForm;
{$R *.res}
Procedure MyButtonClick(Sender:TObject);
begin
ShowMessage('自定义事件');
end;
begin
MyForm1:=TForm.Create(Application);
MyForm1.Show;
MyButton1:=TButton.Create(Application);
MyButton1.Parent:=MyForm1;
MyButton1.OnClick:=MyButtonClick;//此处错误提示:
//Incompatible types:'method pointer and regular procedure'
end. 展开
program Project1;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
var
MyButton1:TButton;
MyForm1:TForm;
{$R *.res}
Procedure MyButtonClick(Sender:TObject);
begin
ShowMessage('自定义事件');
end;
begin
MyForm1:=TForm.Create(Application);
MyForm1.Show;
MyButton1:=TButton.Create(Application);
MyButton1.Parent:=MyForm1;
MyButton1.OnClick:=MyButtonClick;//此处错误提示:
//Incompatible types:'method pointer and regular procedure'
end. 展开
展开全部
MyButtonClick过或迅程需要属于某个对象,才可以;即MyButtonClick需要定义在某个对象内部,且要符慧坦合Procedure (Sender:TObject) 规范;然后再赋值给MyButton1.OnClick。衫碧此
例如:
================================================================
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure Button1Click2(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click2(Sender: TObject);
begin
//
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
button1.on .OnClick:= Button1Click2;
end;
end.
例如:
================================================================
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure Button1Click2(Sender: TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click2(Sender: TObject);
begin
//
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
button1.on .OnClick:= Button1Click2;
end;
end.
追问
就我写的那个测试程序来看,应该怎样调整,因为窗体也是动态生成的。
追答
你可以继承TForm,创建个TForm子类,比如 TBaseForm,然后在TBaseForm中定义一个MyButtonClick,然后:
MyForm1:=TBaseForm.Create(Application);
...
MyButton1.OnClick:=TBaseForm(MyForm1).MyButtonClick;
应该就可以了。
...
展开全部
事件方法必须是类实例的方派空法.
type
TEventHandlers = class { 建一个虚拟类}
锋备procedure MyButtonClick(Sender:TObject);
end;
...
var
尘基瞎 EvHandler:TEventHandlers;
implementation
{方法实现}
procedure TEventHandlers.MyButtonClick(Sender:TObject);
begin
ShowMessage('自定义事件');
end;
{使用}
MyButton1.OnClick:=EvHandler.MyButtonClick;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单测试通过,自尺乎档己稍微陵乱改下
program Project1;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Unit1 in 'Unit1.pas' {Form1};
type lForm = class(TForm)
public
Procedure MyButtonClick(Sender:TObject);
end;
{$R *.res}
var
MyButton1:TButton;
mForm: lForm;
Procedure lForm.MyButtonClick(Sender:TObject);
begin
ShowMessage('自定义事件'顷歼);
end;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
MyButton1:=TButton.Create(Application);
MyButton1.Parent:=Form1;
mForm:= lForm.CreateNew(nil);
MyButton1.OnClick:= mform.MyButtonClick;//
Application.Run;
end.
program Project1;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Unit1 in 'Unit1.pas' {Form1};
type lForm = class(TForm)
public
Procedure MyButtonClick(Sender:TObject);
end;
{$R *.res}
var
MyButton1:TButton;
mForm: lForm;
Procedure lForm.MyButtonClick(Sender:TObject);
begin
ShowMessage('自定义事件'顷歼);
end;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
MyButton1:=TButton.Create(Application);
MyButton1.Parent:=Form1;
mForm:= lForm.CreateNew(nil);
MyButton1.OnClick:= mform.MyButtonClick;//
Application.Run;
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询