有一主窗口MDIForm
通过主窗口上的菜单,先后打开两个子窗口(MDIChild1,MDIChild2).两个子窗口分别有一个LABEL和BUTTON请问如何通过子窗口上的按钮分别控制另一个窗口的LABEL的内容(既MDIChild1.button事件改变MDIChild2.Label显示的内容)有人提议用句柄控制,我是菜鸟不怎么明白,还请各位大虾耐心指导!谢谢!

解决方案 »

  1.   

    program Project2;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1},
      Unit2 in 'Unit2.pas' {Form2},
      Unit3 in 'Unit3.pas' {Form3};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.CreateForm(TForm2, Form2);
      Application.CreateForm(TForm3, Form3);
      Application.Run;
    end.unit Unit2;//子窗口1interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementationuses Unit3;//注意引用2号子窗体{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      form3.Label1.Caption := '内容';
    end;end.
      

  2.   

    在MDIChild1里use MDIChild2试试
      

  3.   

    TO cuilj(一云忆水),你两个窗口都是自动创建的,没有问题.但是我两个子窗口是一个一个的打开的,会出现一个
    "project xxx.exe  raised exception class EaccessViolation with Message 'Access Violation At Address 006769E7 in module xxx.exe'"read of......
      

  4.   

    在MDIChild1里use MDIChild2单元名称,
    然后在MDIChild1的button事件里写
    MDIChild2.Label.Caption := '...';
      

  5.   

    unit frmTemplate1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    const
      MY_WMChanged = WM_User + 100;
    type
      TfMyTemplate = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
        procedure OnChange(var Msg: TMessage); message MY_WMChanged;
      public
        { Public declarations }
      end;var
      fMyTemplate: TfMyTemplate;implementation{$R *.dfm}procedure TfMyTemplate.Button1Click(Sender: TObject);
    var
      i: Integer;
      tmp: string;
    begin
      tmp := Edit1.Text;
      for i := 0 to Screen.FormCount - 1 do
        SendMessage(Screen.Forms[i].Handle, MY_WMChanged, Integer(tmp), Self.Handle);
    end;procedure TfMyTemplate.OnChange(var Msg: TMessage);
    begin
      if self.Handle <> Msg.LParam then
        Label1.Caption := PChar(Msg.WParam);;
    end;procedure TfMyTemplate.FormClose(Sender: TObject;
      var Action: TCloseAction);
    begin
      Action := caFree;
    end;end.