关于一个指针操作 ,在下面的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;