如在VC++.NET 2003 Test托管程序中,有两Form1 和Form2,Form1为父窗体,Form2为子窗体.
Form1中引用#include "Form2.h"头文件
private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
 {
 Test::Form2 * form2 = new Test::Form2();
 form2->MdiParent = this;
 form2->Show();
 }Form2中引用#include "Form1.h"头文件
private: System::Void Form2_Load(System::Object *  sender, System::EventArgs *  e)
 {
 Test::Form1 * form1 ;
 form1 = dynamic_cast<Test::Form1 *>(this->MdiParent);
 form1->Text = "AAA";
 }
 
这样写为什么会出错?该怎样实现父子窗体之间相互调用?

解决方案 »

  1.   

    dynamic_cast需要RTTI支持,但是RTTI对托管代码无效
      

  2.   

    如果只是修改text的话,你可以如下:
    this->MdiParent->Text = "AAA";
      

  3.   

    Test::Form1 * form1 ;
    下面的代码不要,就出错,报FORM1不是TEST成员
    form1 = dynamic_cast<Test::Form1 *>(this->MdiParent);
     form1->Text = "AAA";
      

  4.   

    如果只是修改text的话,你可以如下:
    this->MdiParent->Text = "AAA";
      

  5.   

    this->MdiParent 获取的就是父窗体啊
      

  6.   

    不使用dynamic_cast就是了。直接强制转换就好了。
    (Test::Form1 * )(this->MdiParent)
      

  7.   

    只要这一句就出错,报FORM1不是TEST成员
    Test::Form1 * form1 ;
      

  8.   

    那包含form1.h之后就应该没问题
      

  9.   

    你采用委托的方法来避免Include嵌套,例如:在form2中,如下定义:
    子窗体如下:
    public __delegate void SetTextValueHandler( char* sValue );
    /// <summary> 
    /// Summary for frmChild
    ///
    /// WARNING: If you change the name of this class, you will need to change the 
    ///          'Resource File Name' property for the managed resource compiler tool 
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public __gc class frmChild : public System::Windows::Forms::Form
    {
    public: 
    frmChild(SetTextValueHandler * Handler)
    {
    InitializeComponent();
    mHandler = Handler;
    }
            
    protected: 
    void Dispose(Boolean disposing)
    {
    if (disposing && components)
    {
    components->Dispose();
    }
    __super::Dispose(disposing);
    }
            
    private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container* components;
    SetTextValueHandler* mHandler; /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
    // 
    // frmChild
    // 
    this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    this->ClientSize = System::Drawing::Size(292, 273);
    this->Name = S"frmChild";
    this->Text = S"frmChild";
    this->Load += new System::EventHandler(this, frmChild_Load); }
    private: System::Void frmChild_Load(System::Object *  sender, System::EventArgs *  e)
     {
     this->mHandler( "AAA" );
     }
    };
      

  10.   

    而父窗体可以如下:
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
     {
     frmChild * myChild = new frmChild(
     new SetTextValueHandler( this, &Form1::SetTextValue) );
     myChild->MdiParent = this;
     myChild->Show();
     }  void SetTextValue( char* sValue )
     {
     this->Text = sValue;
     }