就是窗体左边用TREEVIEW组件,右边放个Panel组件,在Treeview上单击一个分支,让Panel上显对应窗体,但点另一个窗体时,当前在Panel上的窗体得关闭,以便下一个窗体打开,请问怎么解决,因为单击另一个窗体时,事先不也确定具体是那个窗体在打开状态,所以不能加from.close

解决方案 »

  1.   

    uses unit2;procedure CloseForm(pnl:TPanel);
    var
      i:Integer;
    begin
      for i:=0 to pnl.ControlCount-1 do
         if pnl.Controls[i] is TForm then
            TForm(pnl.Controls[i]).Close;
    end;procedure TForm1.TreeView1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      tn:TTreeNode;
    begin
      tn:=TreeView1.GetNodeAt(x,y);
      if tn<>nil then
      begin
        CloseForm(panel1);
        if tn.Text='窗体1' then
        begin
           form2:=Tform2.create(self);
           form2.parent:=panel1;
           form2.Left:=0;
           form2.Top:=0;
           form2.Show;
        end
        { else ...
        }
      end;
    end;
      

  2.   

    多谢二楼的,给的思路不错,不过昨天晚上找了好久还找到一思路拿出来给大家分享下,就是声明一个全局变量,当有窗体打开时就把窗体名子赋给这个变量,在另一个窗体要打开时通过判断这个变量是否为空来打开,如果不为空,用变量名加CLOSE的方法关闭也是不错的方法