Ada*_*son 1 delphi event-handling firemonkey
首先在这里发帖所以请原谅任何礼仪失误.
我正在Delphi XE8中创建一个多设备(FMX)应用程序,并且很难为动态创建的按钮分配事件处理程序.我搜索了StackOverflow并找到了与NotifyEvents相关的答案,所以我按照这些答案中的建议 - 仍然没有运气.编译错误是"E2010不兼容的类型:'TNotifyEvent'和'Procedure'".
我已经整理了一个带有编辑字段和静态Hello按钮的表单的简单测试用例,第二个按钮创建了一个Goodbye按钮,并尝试为OnClick事件分配一个过程,但我仍然得到相同的错误.
据我所知,我已经遵循了使程序与TNotifyEvent兼容的所有要求,但即使这个基本示例也会失败并出现相同的错误.我正撞在墙上,所以有人可以让我知道我做错了什么吗?
非常感谢.
unit Dynamic_Button_Test1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, FMX.Edit;
type
TForm1 = class(TForm)
Edit1: TEdit;
Hello: TButton;
Create_GoodBye: TButton;
procedure HelloClick(Sender: TObject);
procedure Create_GoodByeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GoodbyeClick(Sender: TObject) ;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Create_GoodByeClick(Sender: TObject);
var
New_Button : TButton ;
begin
New_Button := TButton.Create( Form1 );
New_Button.Parent := Form1 ;
New_Button.Text := 'Goodbye' ;
New_Button.Visible := True ;
New_Button.Margins.Left := 50 ;
New_Button.Margins.Right := 50 ;
New_Button.Margins.Bottom := 30 ;
New_Button.Height := 50 ;
New_Button.Align := TAlignLayout.Bottom ;
New_Button.OnClick := TForm1.GoodbyeClick ;
end;
procedure TForm1.HelloClick(Sender: TObject);
begin
Edit1.Text := 'Hello' ;
end;
procedure TForm1.GoodbyeClick(Sender: TObject);
begin
Edit1.Text := 'Goodbye' ;
end;
end.
Run Code Online (Sandbox Code Playgroud)
VCL/FMX事件处理程序在运行时绑定到特定对象.分配事件处理程序时,需要使用对象指针替换类typename.Self当事件稍后触发时,该对象将成为事件处理程序的指针:
New_Button.OnClick := Self.GoodbyeClick ;
Run Code Online (Sandbox Code Playgroud)
或者干脆:
New_Button.OnClick := GoodbyeClick ; // Self implicitly used
Run Code Online (Sandbox Code Playgroud)
在旁注 - 创建Button时,该代码在TForm1实例方法内部,因此您应该使用Self对象指针而不是全局Form1对象指针:
New_Button := TButton.Create( Self );
New_Button.Parent := Self ;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4819 次 |
| 最近记录: |