有如下一段程序,运行时,在第一个窗口按按钮能打开第二个窗口,在第二个窗口按第一个按钮能打开第三个窗口,但在第二个窗口按第二个按钮不能打开第四个窗口,不知何解,望有高手能指出。
program Project1;uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3},
  Unit4 in 'Unit4.pas' {Form4};{$R *.res}begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.//第一单元unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    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
form2 := TForm2.Create(self);
form2.ShowModal;
form2.free;
end;end.
//第二单元unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls,StdCtrls;type
  TForm2 = class(TForm)
    Button1: TButton;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementation
 uses unit3, unit4;
{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
  form3 : TForm3;
begin
form3 := TForm3.Create(self);
form3.ShowModal;
form3.free;
end;procedure TForm2.Button2Click(Sender: TObject);
var
  form4 : TForm4;
begin
form4 := TForm4.Create(self);
form4.ShowModal;
form4.free;
end;end.
//第三单元
unit Unit3;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm3 = class(TForm)
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form3: TForm3;implementation{$R *.dfm}end.
//第四单元unit Unit4;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm4 = class(TForm)
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form4: TForm4;implementation{$R *.dfm}end.

解决方案 »

  1.   

    没有发现你的问题,同样可以打开第四个窗口。不过按第一个按钮打开第三个窗口后,由于你是用ShowModal,所以你必须先关闭第三个窗口后,才能再去第二个窗口按第二个按钮打开第四个窗口。如果要同时打开第三、第四个窗口,可以用Form3.Show;(Form3.Free要去掉才能使第三个窗口不马上消失)显示第三个窗口,这时可再去第二个窗口按第二个按钮打开第四个窗口。
      

  2.   

    没有问题,不过这些定义都可以省去
    var
      form2 : TForm2;
      form3 : TForm3;
      form4 : TForm4;
      

  3.   

    一楼:我不按第一按钮就直接按第二个按钮的,所以不用关闭第三个窗口。四楼:把所有的self都变成nil试过了,还是不行。
    高手们继续帮忙找找原因。
      

  4.   

    你的第二个按钮没有关联事件到Button2Click吧?