代码1:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TReadThread=class(TThread)
private
Faa:string;
protected
procedure Execute;override;
public
constructor Create(aa:string);
destructor Destroy; override;
end;
var
Form1: TForm1;implementation{$R *.dfm}{ TReadThread }constructor TReadThread.Create(aa: string);
begin
Faa:=aa;
FreeOnTerminate:=True;
inherited Create(false);
end;destructor TReadThread.Destroy;
begin
Faa:='';
inherited;
end;procedure TReadThread.Execute;
begin
form1.Memo1.Lines.Add(Faa);end;procedure TForm1.Button1Click(Sender: TObject);
begin
TReadThread.Create('asdf');
end;end.这个时候点击按钮向Memo加入‘asdf’没有任何问题
但是在form2上动态创建后会出问题
在form1.Memo1.Lines.Add(Faa);断点看到 form1.Memo1=Inaccessible value
想知道为什么会出现这种结果
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Unit1, StdCtrls;type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var bb:TForm1;
begin
bb:=TForm1.Create(self);
bb.Parent:=Form2;
bb.Show;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TReadThread=class(TThread)
private
Faa:string;
protected
procedure Execute;override;
public
constructor Create(aa:string);
destructor Destroy; override;
end;
var
Form1: TForm1;implementation{$R *.dfm}{ TReadThread }constructor TReadThread.Create(aa: string);
begin
Faa:=aa;
FreeOnTerminate:=True;
inherited Create(false);
end;destructor TReadThread.Destroy;
begin
Faa:='';
inherited;
end;procedure TReadThread.Execute;
begin
form1.Memo1.Lines.Add(Faa);end;procedure TForm1.Button1Click(Sender: TObject);
begin
TReadThread.Create('asdf');
end;end.这个时候点击按钮向Memo加入‘asdf’没有任何问题
但是在form2上动态创建后会出问题
在form1.Memo1.Lines.Add(Faa);断点看到 form1.Memo1=Inaccessible value
想知道为什么会出现这种结果
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Unit1, StdCtrls;type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var bb:TForm1;
begin
bb:=TForm1.Create(self);
bb.Parent:=Form2;
bb.Show;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TReadThread=class(TThread)
private
faa: string;
FForm: TForm1;
protected
procedure Execute;override;
public
constructor Create(CreateSuspended: Boolean);
property aa: string read faa write faa;
property AForm: TForm1 read FForm write FForm;
end;var
Form1: TForm1;implementation{$R *.dfm}constructor TReadThread.Create(CreateSuspended: Boolean);
begin
inherited Create(false);
end;procedure TReadThread.Execute;
begin
FreeOnTerminate:=True;
FForm.Memo1.Lines.Add(Faa);
end;procedure TForm1.Button1Click(Sender: TObject);
var
rt: TReadThread;
begin
rt:= TReadThread.Create(false);
rt.aa:= 'asdf';
rt.AForm:= self;
rt.Resume;
end;end.