第二个窗体我已经让它被动创建,不自动创建,它的全局变量我也注释掉了
还有Form2 := nil;怎么能行了,都没有Form2的变量.
完整的代码如下:
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Form2: TForm2;
begin
if Assigned(Form2) then
begin
ShowMessage('ÎÒÒѾ­´æÔÚÁË,ÎÒµÄÃû×ÖÊÇ: '+Form2.Name);
Form2.Show;
end else begin
ShowMessage('ÎÒÒª´´½¨ÁË');
Form2:=TForm2.Create(Application);
Form2.Show;
end;
end;
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
end.
错误信息:[Warning] Unit1.pas(30): Variable 'Form2' might not have been initialized