System.Windows.Forms.Form mForm;
改为:
Type mFormType;点击的时候把Form的Type传进去实例化Form:Form frm = (Form)Activator.CreateInstance( mFormType );frm.Show();//销毁...

解决方案 »

  1.   

    to timmy3310(tim):
    这里的mFormType是一个字符串吗?那Type又是什么呢?能使用我上面内的代码提供一个实例吗?
      

  2.   

    class myMenuItem:System.Windows.Forms.MenuItem
    {
       ...
       System.Windows.Forms.Form mForm;
       System.Type mFormType; // 增加一个Form类型的属性
       ...
    }class OtherForm:System.Window.Form
    {
      ...
    }class myForm:System.Window.Form
    {
      ...
      myForm_Load(..)
      {
        System.Windows.Forms.MainMenu myMainMenu;
        myMenuItem tmpMI;
        this.MainMenu=myMainMenu=new System.Windows.Forms.MainMenu();
        myMainMenu.MenuItems.Add(tmpMI=new myMenuItem(..));
        tmpMI.Click+=new System.EventHandler(this.miClick);
        this.MiAddForm(tmpMI,new OtherForm());
      }
      ...
      private void miClick(object sender,System.EventArgs e)
      {
        myMenuItem tmpMI=(myMenuItem)sender;
        if(tmpMI.mForm已实例化)
        {
          tmpMI.mForm.Activate();//激活
          return;
        }
        //以下该如何解决mForm未实例化的问题?
        //不能使用tmpMI.mForm=new OtherForm(),因为OtherForm在变
        //使用tmpMI.mForm=new (TypeOf(tmpMI.mForm))()报错
        else
        {
          tmpMI.mForm = (Form)Activator.CreateInstance(tmpMI.mFormType);  //这里根据Form的类型,动态创建
        }
        tmpMI.mForm.Show();
      }
      private void MiAddForm(myMenuItem mi,System.Windows.Forms.Form frm)
      {
        mi.mForm=frm;
        mi.mFormType = frm.GetType();
        frm.MDIParent=this;
        frm.Dispose();
      }
      ...
    }