我今天看到一个简单的程序,就是有三个窗口,FORM2作为主窗口,其余两个作为可引用的窗口,在FORM2表单中有 use unit3,但没有use unit1,可以在程序中照样引用了form1的窗口变量,这不知为什么,我试着去掉了use unit3,结果引用FORM3变量的语句报错,但为什么引用 form1的窗口变量的语句就不会报错呢?

解决方案 »

  1.   

    unit1的变量位置?
    unit1的引用方法?
      

  2.   

    USES可在两个地方:interface和implementation后,你仔细看一下CODE是不是FORM1已经USES在implementation后了.
      

  3.   

    你可以把代码定义在窗口类的public里边,这样的变量才是真正的全局变量,可以在任意地点引用
      

  4.   

    兄弟们:代码来也,就是长了点,我先说明一下:unit2单元是主窗体,我不理解的是并没有引用unit1,可怎么会显示unit1的窗口。这是主窗体的代码:
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Buttons, Unit3;type
      TForm2 = class(TForm)
        BitBtn1: TBitBtn;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        BitBtn2: TBitBtn;
        Label4: TLabel;
        BitBtn3: TBitBtn;
        BitBtn4: TBitBtn;
        procedure Button1Click(Sender: TObject);
        procedure BitBtn1Click(Sender: TObject);
        procedure BitBtn2Click(Sender: TObject);
        procedure BitBtn3Click(Sender: TObject);
        procedure BitBtn4Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;
      AboutBox: TAboutBox;implementationuses Unit1;{$R *.DFM}procedure TForm2.Button1Click(Sender: TObject);
    begin
        MessageDlg('This button just gives you something to look at when the application starts.', mtInformation,
            [mbOk], 0);end;procedure TForm2.BitBtn1Click(Sender: TObject);
    begin
        close;
    end;procedure TForm2.BitBtn2Click(Sender: TObject);
    begin
      AboutBox := TAboutBox.Create(Application);
      AboutBox.ShowModal;
    end;procedure TForm2.BitBtn3Click(Sender: TObject);
    begin
      Form_Splash := TForm_Splash.Create(Application);  {Builds the actual Splash Form}
      Form_Splash.Show;
      Form2.Show;
    end;procedure TForm2.BitBtn4Click(Sender: TObject);
    begin
       Form_Splash.Close;
    end;end.这是unit3的代码:
    unit Unit3;interfaceuses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
      Buttons, ExtCtrls;type
      TAboutBox = class(TForm)
        Panel1: TPanel;
        ProgramIcon: TImage;
        ProductName: TLabel;
        Version: TLabel;
        Copyright: TLabel;
        Comments: TLabel;
        OKButton: TButton;
        procedure OKButtonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      AboutBox: TAboutBox;implementation{$R *.DFM}procedure TAboutBox.OKButtonClick(Sender: TObject);
    begin
         close;
    end;end.这是unit1的代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls;type
      TForm_Splash = class(TForm)
        Image1: TImage;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form_Splash: TForm_Splash;implementation{$R *.DFM}procedure TForm_Splash.FormClose(Sender: TObject;
      var Action: TCloseAction);
    begin
     Action := caFree; {further ensures the resources for your Splash Form are freed}
    end;end.下面是项目文件:
    program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form_Splash},
      Unit2 in 'Unit2.pas' {Form2},
      Unit3 in 'Unit3.pas' {AboutBox};{$R *.RES}begin
      Application.Initialize;
      Form_Splash := TForm_Splash.Create(Application);  {Builds the actual Splash Form}
      Form_Splash.Show;   {Sets the Splash screen to show, but since the application
                           is still initializing, it won't actually show until . . .}
      Form_Splash.Refresh; {Until you do this command!}
      Application.CreateForm(TForm2, Form2);
      Form_Splash.Hide;    {Hides the Splash Form, allows your Main Form to be displayed}
      Form_Splash.Free;    {Releases the resources used by your Splash Form! Important!!}
      Application.Run;
    end.