关于procedure of Object

  • 主题发起人 主题发起人 import
  • 开始时间 开始时间
I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
关于一个指针操作 ,在下面的TreeView1OnClick中错误,为什么?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
private
{ Private declarations }
i:integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
F:procedure (Sender:TObject) of Object;
begin
F:=Button2Click;
TreeView1.Items.AddObject(nil,'test',@F);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Inc(Form1.i);//这个Form1是一定用加的!
Form1.Caption:=Sender.ClassName +' 调用次数'+IntToStr(Form1.I);
end;
procedure TForm1.TreeView1Click(Sender: TObject);
var
F:Procedure (Sender:TObject) of Object;
begin
if TreeView1.Selected.Data<>nil then
begin
F:=nil;
@F:=TreeView1.Selected.Data;
if @F=@Button2Click then //Variable required !!! 在这里会出错,为什么?
begin
F(Sender);
end;
F:=nil;
end;
end;
end.
*******************************
解决如下:
procedure TForm1.Button1Click(Sender: TObject);
var
F:procedure (Sender:TObject) of Object;
begin
F:=Button2Click;
TreeView1.Items.AddObject(nil,'test',@F);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Inc(Form1.i);//这个Form1是一定用加的!
Form1.Caption:=Sender.ClassName +' 调用次数'+IntToStr(Form1.I);
end;
procedure TForm1.TreeView1Click(Sender: TObject);
var
F:Procedure (Sender:TObject) of Object;
begin
if TreeView1.Selected.Data<>nil then
begin
F:=nil;
@F:=TreeView1.Selected.Data;
if @F=@Button2.OnClick then //Variable required !!! 在这里会出错,为什么?
begin
F(Sender);
end;
F:=nil;
end;
end;
 

Similar threads

A
回复
0
查看
971
Andreas Hausladen
A
S
回复
0
查看
678
SUNSTONE的Delphi笔记
S
S
回复
0
查看
672
SUNSTONE的Delphi笔记
S
A
回复
0
查看
955
Andreas Hausladen
A
后退
顶部