RT
谢谢

解决方案 »

  1.   

    看看这个helper类能不能对你有点帮助:
    public class FromUtil 
    { /// <summary> 
    /// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立 
    /// </summary> 
    /// <param name="MDIForm"></param> 
    /// <param name="ChildForm"></param> 
    public static void ActionChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm) 

    foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) 

    if (ChildForm.GetType() == f.GetType()) 

    ChildForm = null; 
    f.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
    f.Show(); 
    f.Activate(); 
    //break; 
    return; 


    ChildForm.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
    ChildForm.MdiParent = MDIForm; 
    ChildForm.Show(); } 
    /// <summary> 
    /// 在MDI窗体中寻找ChildForm,如果childform已经存在则激活为当前子窗体,如果子窗体不存在则建立 
    /// 同时关闭其它子窗体 
    /// </summary> 
    /// <param name="MDIForm"></param> 
    /// <param name="ChildForm"></param> 
    public static void ActionSingleChildForm(System.Windows.Forms.Form MDIForm, System.Windows.Forms.Form ChildForm) 

    foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) 

    if (ChildForm.GetType() == f.GetType()) 

    ChildForm = null; 
    f.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
    f.Show(); 
    f.Activate(); 

    else 

    f.Close(); 



      

  2.   

    if (this.ActiveMdiChild.Name == "dp") 
                { 
                    dplanmain f = (dp)this.ActiveMdiChild; 
                } 
    可不可以不要判断name,弄个通用的写法(不管那个子窗体都可适用)
      

  3.   

    通用的写法,那你的“不管哪个子窗体”都有“通用”的操作方法了?一个类继承下来的?那就直接把ActiveMdiChild转换成父类,调用父类的方法喽。
      

  4.   

    foreach (System.Windows.Forms.Form f in MDIForm.MdiChildren) 
    可能找到你的所有窗体,你可以根据条件激活
    你可以参 http://www.cnblogs.com/wts/archive/2007/03/15/675806.html
      

  5.   

    Form1 fm = (Form1)this.ActiveMdiChild ;
    然后操作fm就是了
      

  6.   

    举个例子
    有一个主窗口 Mainform.cs
    两个子窗口 Form1和 Form2 Mainform.cs//用全局变量来控制
    static class Common
    {
        public static Form1 sform1 = null;
        public static Form2 sform2 = null;

    }
    //在Mainform.cs中 添加menustrip
    //并创建两个子菜单ViewMenu和WindowsMenu
    //主菜单menuStrip1中将MdiWindowListItem设置成WindowsMenu this.menuStrip1.MdiWindowListItem = this.windowToolStripMenuItem;//在ViewMenu中添加2个子选项form1ToolStripMenuItem和form2ToolStripMenuItem
    //并且添加click事件
     this.form1ToolStripMenuItem.Click += new System.EventHandler(this.form1ToolStripMenuItem_Click);
                this.form2ToolStripMenuItem.Click += new System.EventHandler(this.form2ToolStripMenuItem_Click);

    //这里写一个form1ToolStripMenuItem的click事件,form2ToolStripMenuItem和它相同做法
       private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (Common.sform1.Visible == true)
                {
                    Common.sform1.Visible = false;
                }
                else
                {
                    Common.sform1.ReVisible();
                }
            }
    //然后给Mainform追加一个FormClosing事件和Load事件
    //创建子窗体
      private void Mainform_Load(object sender, EventArgs e)
            {
                Common.sform1 = new Form1();
                Common.sform1.MdiParent = this;
                Common.sform2 = new Form2();
                Common.sform2.MdiParent = this;
            }
    //关闭所有子窗体
     private void Mainform_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = false;
            }//————————————————————————————————————————————————
    //Form1
    //添加引用
    using System.Reflection;
    //显示隐藏的子窗体  public void ReVisible() {
                FieldInfo fi = typeof(Form).GetField("closeChild",
                                                     BindingFlags.Instance | BindingFlags.NonPublic);
                fi.SetValue(this, CloseReason.None);
                this.Visible = true;
            }
    //formClosing事件,这个关闭只相当于将子窗体隐藏        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = true;
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    this.Visible = false;
                }
            }
    //这样你可以控制Common.sform1 来控制子窗体
    //并且可以通过菜单ViewMenu来控制子窗体的打开和关闭,
    //通过菜单WindowsMenu来控制打开的子窗体,哪个前端显示
    //以上是部分代码,希望对你有帮助
      

  7.   

    FieldInfo fi = typeof(Form).GetField("closeChild",
                                                     BindingFlags.Instance | BindingFlags.NonPublic);

    这里写错了应该是GetField("closeReason"
    对不起写太快了没检查