我在主窗体中动态创建子窗体,主窗体上通过tmainmenu的菜单项来建立不同的子窗,子窗体显示在一个TPanel中(代码中的ContainerPanel),现在的问题是子窗体可以建立,但在show的过程中,会自动调用子窗体的onDeletion事件,结果把子窗体中Treeview中的Data全清掉,主窗体中的代码如下:
procedure TmainForm.N3Click(Sender: TObject);
begin
  ShowForms(TchildForm1); //TchildForm1是子窗体的type名
end;
procedure TmainForm.ShowForms(NewForm: TFormClass);
var NewDemo: TForm;
begin
  if (ContainerPanel.ControlCount=0) or not (ContainerPanel.Controls[0] is NewForm) then
  begin
    if ContainerPanel.ControlCount > 0 then ContainerPanel.Controls[0].Free;
    if Assigned(NewForm) then
    begin
      NewDemo := NewForm.Create(nil);
      //调试时发现每次运行到这里后,都要去调用子窗体中的TreeView1Deletion,害得我在子窗体中无法使用Node.Data,为什么?如何解决?
      NewDemo.Hide;
      NewDemo.BorderStyle := bsNone;
      NewDemo.Parent := ContainerPanel;
      NewDemo.Align := alClient;
      NewDemo.Show;
    end;
  end;
end;
子窗体中有一个treeview1,写了一个OnDeletion事件:
procedure TchildForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
  Dispose(Node.Data);
end;

解决方案 »

  1.   

    试下NewDemo := NewForm.Create(application);
      

  2.   

    将OnDeletion事件代码在创建后再赋值
    procedure MyDeletion(Sender: TObject; Node: TTreeNode);
    begin
      Dispose(Node.Data);
    end;NewDemo := NewForm.Create(nil);
      NewDemo.Treeview.OnDeletion := MyDeltion;
      ...
      

  3.   

    我调试了一下,其实出现问题的是:NewDemo.Hide;这句,并不是在create里面。把它注释调就没有问题了。
      

  4.   

    还有这句:
    NewDemo.BorderStyle := bsNone;
    要么在TreeView1Deletion加个判断,要么是事先设计好form的BorderStyle