访问用PageControl封装的窗体变量报内存错误,请教高人

richardi 2012-08-20 10:23:37
我用PageControl封装窗体,当窗体创建后,再读取已创建的窗体变量时报内存错误,下面是所有代码:

program ShowFormDemo;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2}

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1); //注意,这里只创建Form1,其它窗体动态创建,如果都先创建就不报错
Application.Run;
end.


//下面是主窗体
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
PageControl1: TPageControl;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure ShowForm(AClass:TComponentClass;AForm:TForm);
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses Unit2,Unit3;
{$R *.dfm}
procedure TForm1.ShowForm(AClass:TComponentClass;AForm:TForm);//封装窗体的函数
var Tabsht: TTabSheet;
begin
Tabsht := TTabSheet.Create(Application);
Tabsht.PageControl:=PageControl1;
application.CreateForm(AClass,AForm);
AForm.Parent:=Tabsht;
Tabsht.Tag:=Integer(AForm);
AForm.BorderStyle:=bsNone;
AForm.Align:=alClient;
AForm.show;
Tabsht.Caption:=AForm.Caption;
PageControl1.ActivePage := Tabsht;
Tabsht.Show;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowForm(TForm2,Form2);
Form2.FVal:=100;//这里报错
end;
end.

//下面是Form2窗体
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
FVal:Integer;//只定义一个变量
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

end.

如果2个窗体在工程文件中都先创建好了,就不会报错,我怀疑在ShowForm(TForm2,Form2)后,Form2是局部变量,导致访问出错,可是我创建时用的application.CreateForm(AClass,AForm);它的owner应该是Application这个全局对象啊,怎么回出错呢,请高人指点,谢谢。
...全文
95 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kaikai_kk 2012-08-23
ShowForm用var

procedure ShowForm(AClass:TComponentClass;var AForm:TForm);
  • 打赏
  • 举报
回复
jAmEs_ 2012-08-23
[Quote=引用 1 楼 的回复:]

请按下面代码修改:
Delphi(Pascal) code

......
public
{ Public declarations }
procedure ShowForm(AClass:TComponentClass;var AForm:TForm);
end;

var
Form1: TForm1;

implementation
uses Unit2,Un……
[/Quote]这个对lz意义大些
  • 打赏
  • 举报
回复
广州佬 2012-08-20
又或者这样改(但很明显,不符合你的用途):

......

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Unit2, Unit3;

......

public
{ Public declarations }
procedure ShowForm(AClass:TComponentClass; var AForm:TForm2);
end;

var
Form1: TForm1;

implementation
//uses Unit2,Unit3;

{$R *.dfm}

......

procedure TForm1.ShowForm(AClass: TComponentClass; var AForm: TForm2);
var Tabsht: TTabSheet;
begin
......
end;

......

  • 打赏
  • 举报
回复
广州佬 2012-08-20
或者,你原来的其他代码不变,仅将点击按钮事件代码按下面修改,就明白原因了:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowForm(TForm2,Form2);
if Form2<>nil then
Form2.FVal:=100//这里报错
else
showmessage('Form2的实例不存在。');
end;
  • 打赏
  • 举报
回复
广州佬 2012-08-20
请按下面代码修改:

......
public
{ Public declarations }
procedure ShowForm(AClass:TComponentClass;var AForm:TForm);
end;

var
Form1: TForm1;

implementation
uses Unit2,Unit3;


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Fm2:TForm;
begin
ShowForm(TForm2,Fm2);
TForm2(Fm2).FVal:=100;
end;

procedure TForm1.ShowForm(AClass: TComponentClass;var AForm: TForm);
var Tabsht: TTabSheet;
begin
......
end;

......
  • 打赏
  • 举报
回复
发帖
Delphi

5124

社区成员

Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
帖子事件
创建了帖子
2012-08-20 10:23
社区公告
暂无公告