有2个窗体,窗体1中创建了窗体2,然后用showmodul显示它.想问在窗体2中有个名称为"应用"的按钮想控制窗体1中窗体的状态(好比颜色)可以实现么?如何实现.
注意:1.是showmodul显示.我用消息传递,但是好像会出异常,所以如果showmodul不行,那么模拟实现的也可以;
2.想要告诉在按钮点击处理中加入form1.color=...的请先思考如何避免"Circular unit reference "的错误.

解决方案 »

  1.   

    unit1procedure TForm1.Button1Click(Sender: TObject);
    begin
      Application.CreateForm(TForm2, Form2);
      Form2.ShowModal;
      Form2.Free;
    end;
    unit2
    procedure TForm2.Button1Click(Sender: TObject);
    begin
      Form1.Color := clBlue;
    end;不会出错呀!!!!!!!!!
      

  2.   

    两个都在implementation 下面 或一个在 implementation 一个在 interface 下面
      uses 另一窗体的单元文件
    再在按钮点击处理中加入form1.color=...
      

  3.   

    //窗体1
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Menus, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}//打开From2
    procedure TForm1.Button1Click(Sender: TObject);
    var
      FrmColor:TColor;
    begin
      FrmColor := Form1.Color;
      if OpenFrm2(FrmColor) then
        Form1.Color := FrmColor;
    end;end.//窗体2
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        ColorBox1: TColorBox;
        procedure Button1Click(Sender: TObject);
      private
        bReturn: Boolean;
        MyColor: TColor;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;
      function OpenFrm2(var c:TColor):Boolean;
    implementationfunction OpenFrm2(var c:TColor):Boolean;
    var
      Frm2: TForm2;
    begin
      try
        Frm2 := TForm2.Create(nil);
        Frm2.ShowModal;
        c := Frm2.MyColor;
        result := Frm2.bReturn;
      finally
        Frm2.Free;
        Frm2 := nil;
      end;
    end;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      MyColor := ColorBox1.Selected;
      bReturn := True;
      close;
    end;end.
      

  4.   

    用postmessage应该是可以的。但在按钮点击事件中最好不要用form1.color=,应用postmessage发送消息,在form1中定义函数来接收事件。
      

  5.   


     luke5678(奇异)所写的就可以了吧,
     一个那样的问题,有必要用消息吗?
     这样是最简单的了
      

  6.   

    阿飞说的对~奇异写的有道理
    但是可能是问题写的不清楚,我想实现"应用"这个功能,也就是Form2在不关闭的情况下通过"应用"来调整Form1的颜色.否则的话直接在Form1的procedure TForm1.Button1Click(Sender: TObject);加入:
    if Form2.ShowModal=2 then self.Color:=Form2.ColorBox1.Selected;
    不过这个问题经大家的点拨还是解决了,关键在于meiqingsong(阿飛):两个都在implementation 下面 或一个在 implementation 一个在 interface 下面uses 另一窗体的单元文件;
    代码如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, unit2, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    form2.ShowModal;
    end;end.
    -------------------------------------------------------------
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm2 = class(TForm)
        ColorBox1: TColorBox;
        Button1: TButton;
        procedure ColorBox1Change(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}
    uses
    unit1;procedure TForm2.ColorBox1Change(Sender: TObject);
    begin
    form1.Color:=self.ColorBox1.Selected;
    end;end.
      

  7.   

    var
      Form1: TForm1;implementation
      uses unit2;
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin 
     form2.showmodal;
     end;
    end.
    var
      Form2: TForm2;implementation
    uses unit1;
    {$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
        if ColorDialog1.Execute then
        form1.Color:=ColorDialog1.Color;
    end;end.Windows2000+D6測試通過~~~