小弟新手,请教打开多个MDI子窗体的方法~
使类似下面的代码不重复出现,最好可以包含使限制每个窗体只打开一个的功能。mdiform1 mdiForm =new mform1();
mdiForm.MdiParent=this;
mdiForm.show();
mdiForm.focus; 谢谢!

解决方案 »

  1.   

    使类似下面的代码不重复出现??什么意思?
    每个窗体只打开一个的功能倒是知道怎么实现:
    用一个循环判断:
    foreach (Form anForm in this.MdiChildren)
                {
                    if (anForm.Name == "窗体名称")
                    {
                        anForm.WindowState = FormWindowState.Normal;
                    }
                    else
                    {
                        Form1 aForm = new Form1();
                        aForm.Parent = this;
                        aForm.Show();
                    }
                }
      

  2.   

               Form newFrm = new Form1();
                foreach (Form child in this.MdiChildren)
                {
                    if (child.Text.Trim() == newFrm.Text.Trim())
                    {
                        child.Activate();
                        child.Focus();
                        return;
                    }
                }
                newFrm.MdiParent= this;
                newFrm.Show();
                newFrm.WindowState = FormWindowState.Maximized;可以写成一个公共的方法。以后每次调用就好了
      

  3.   

    用单例模式啊
    static Form instance=null;public static Form Instance
    {
    get
    {
    if (instance==null)
    {
    Form form1= new Form();
    }
    return instance;
    }
    }
      

  4.   

    谢谢各位了啊~
    我写成这样了:
    form1 newForm =new form1();
    openForm(newForm);有什么办法可以把
    form1 newForm =new form1();
    也加到方法openForm()里去么??
      

  5.   

      private void button2_Click(object sender, EventArgs e)
            {
                OpenForm("test.Form6", "test.exe");
            }private void OpenForm(string strForm, string strFile)
            {
                Form frmCur = null;
                if (string.IsNullOrEmpty(strFile)) frmCur = this;
                else if (frmCur == null || frmCur.IsDisposed)
                {
                    Assembly assDLL = Assembly.LoadFrom(Application.StartupPath + "\\" + strFile);
                    if (assDLL == null)
                        throw new Exception("找不到文件" + strFile);
                    frmCur = (Form)assDLL.CreateInstance(strForm);
                    if (frmCur == null)
                        throw new Exception("在文件" + strFile + "中找不到" + strForm);
                    frmCur.Show();
                    frmCur.MdiParent = this;
                }
            }
      

  6.   

    改进一下:
     private void button2_Click(object sender, EventArgs e)
            {
                OpenForm("test.Form6", "test.exe");
            }        private void OpenForm(string strForm, string strFile)
            {
                if (this.MdiChildren.Length == 0)
                    CreateForm(strForm, strFile);
                else
                {
                    foreach (Form child in this.MdiChildren)
                    {
                        if (!strForm.Equals(child.Name))
                        {
                            CreateForm(strForm, strFile);
                        }
                    }
                }
            }        private void CreateForm(string strForm, string strFile)
            {
                Form frmCur = null;
                if (string.IsNullOrEmpty(strFile)) frmCur = this;
                else if (frmCur == null || frmCur.IsDisposed)
                {
                    Assembly assDLL = Assembly.LoadFrom(Application.StartupPath + "\\" + strFile);
                    if (assDLL == null)
                        throw new Exception("找不到文件" + strFile);
                    frmCur = (Form)assDLL.CreateInstance(strForm);
                    if (frmCur == null)
                        throw new Exception("在文件" + strFile + "中找不到" + strForm);            }
                frmCur.Show();
                frmCur.Name = strForm;
                frmCur.MdiParent = this;
            }