我用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;interfaceuses
  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;interfaceuses
  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这个全局对象啊,怎么回出错呢,请高人指点,谢谢。

解决方案 »

  1.   

    请按下面代码修改:......
      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;......
      

  2.   

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

  3.   

    又或者这样改(但很明显,不符合你的用途):......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;......
      

  4.   

    ShowForm用varprocedure ShowForm(AClass:TComponentClass;var AForm:TForm);