i am create one mdi application in c# dot net.
mdi form contain many child forms when open one child form and again open this child form then open 2 times this child form please help me to open one child form at a time in this application

Recommended Answers

All 6 Replies

Try this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.mdi
{
  public partial class frmMain : Form
  {
    public frmMain()
    {
      InitializeComponent();
    }

    private Form OpenForm(Type t)
    {
      if (!t.IsSubclassOf(typeof(Form)) && !(t == typeof(Form)))
        throw new ArgumentException("Type is not a form", "t");

      List<Form> lst = new List<Form>();

      try
      {
        for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
        {
          Form f = Application.OpenForms[i1];
          if (f.IsMdiChild)
            lst.Add(f);
        }
      }
      catch (IndexOutOfRangeException)
      {
        //This can change if they close/open a form while code is running. Just throw it away
      }

      while (lst.Count > 0)
      {
        Form f = lst[0];
        f.Close();
        f.Dispose();
        lst.RemoveAt(0);
      }

      Form result = (Form)Activator.CreateInstance(t);
      result.MdiParent = this;
      result.Show();
      return result;
    }

    private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      frmMdi1 frm = (frmMdi1)OpenForm(typeof(frmMdi1));
      frm.Text = "abc";
    }

    private void openForm2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      frmMdi2 frm = (frmMdi2)OpenForm(typeof(frmMdi2));
      frm.Text = "123";
    }
  }
}

Unless you meant "Only allow each type of form to be opened once" then you could do this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.mdi
{
  public partial class frmMain2 : Form
  {
    public frmMain2()
    {
      InitializeComponent();
    }


    private bool IsFormOpen(Type t)
    {
      if (!t.IsSubclassOf(typeof(Form)) && !(t == typeof(Form)))
        throw new ArgumentException("Type is not a form", "t");

      try
      {
        for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
        {
          Form f = Application.OpenForms[i1];
          if (t.IsInstanceOfType(f))
            return true;
        }
      }
      catch (IndexOutOfRangeException)
      {
        //This can change if they close/open a form while code is running. Just throw it away
      }
      return false;
    }

    private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (!IsFormOpen(typeof(frmMdi1)))
      {
        frmMdi1 frm = new frmMdi1();
        frm.MdiParent = this;
        frm.Show();
      }
    }

    private void openForm2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (!IsFormOpen(typeof(frmMdi2)))
      {
        frmMdi2 frm = new frmMdi2();
        frm.MdiParent = this;
        frm.Show();
      }
    }
  }
}

Thanks,
The given code works perfectly, But now if multiple windows are opened then not getting focus on the already open window. Can anyone suggest how to get the focus on the active child form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.mdi
{
  public partial class frmMain2 : Form
  {
    public frmMain2()
    {
      InitializeComponent();
    }

    private bool IsFormOpen(Type t)
    {
      return IsFormOpen(t, false);
    }
    private bool IsFormOpen(Type t, bool focus)
    {
      if (!t.IsSubclassOf(typeof(Form)) && !(t == typeof(Form)))
        throw new ArgumentException("Type is not a form", "t");

      try
      {
        for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
        {
          Form f = Application.OpenForms[i1];
          if (t.IsInstanceOfType(f))
          {
            if (focus)
            {
              f.BringToFront();
              f.Focus();
            }
            return true;
          }
        }
      }
      catch (IndexOutOfRangeException)
      {
        //This can change if they close/open a form while code is running. Just throw it away
      }
      return false;
    }

    private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (!IsFormOpen(typeof(frmMdi1), true))
      {
        frmMdi1 frm = new frmMdi1();
        frm.MdiParent = this;
        frm.Show();
      }
    }

    private void openForm2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (!IsFormOpen(typeof(frmMdi2), true))
      {
        frmMdi2 frm = new frmMdi2();
        frm.MdiParent = this;
        frm.Show();
      }
    }
  }
}

hello sknake i already want to use this code but there is some problem

Cannot implicitly convert type 'System.Windows.Forms.Form' to 'Hospital_point.Administrator'. An explicit conversion exists (are you missing a cast?)
at line

Administrator f = Application.OpenForms[i1];

hello sknake i already want to use this code but there is some problem

Cannot implicitly convert type 'System.Windows.Forms.Form' to 'Hospital_point.Administrator'. An explicit conversion exists (are you missing a cast?)
at line

Administrator f = Application.OpenForms[i1];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.