Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an MDI parent form that holds my mdi child forms and what I am trying to achieve is to stop multiple instances of the children form from opening. So far I have been able to achieve this i.e when a child form instance is open, another instance of the child form cannot be open. The problem I have now is, when i close the child form, it does not open again until i restart the application. Below is a snippet from my code so far.

C#
public partial class DataMigrationMDI : Form
   {

       ConnectToSource src_mdi_child = null;

       public DataMigrationMDI()
       {
           InitializeComponent();

       }

        private void connectToSourceDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (src_mdi_child == null)
           {
               src_mdi_child = new ConnectToSource();
               src_mdi_child.MdiParent = this;
               src_mdi_child.Show();
           }

       }

   }

So,basically I initially set the child form instance to null and then at the onclick event of the toolstrip i check if the child form is null and then opens it. But with this code it doesn't check when the child form is closed to re-assign it back to null so that in this way the child form can be re-opened. So the help I need is to be shown how to check when a child form has been closed and then assign it back to null. Thanks in advance
Posted
Updated 11-Oct-13 1:27am
v4

Hello Uzoma

Call this function before the code opening the any child,
private void CloseMdichilds()
        {
            this.MdiParent.MdiChildren.OfType<Form>().ToList().ForEach(x => x.Close());
        }
 
Share this answer
 
Comments
Uzoma Umekwe 11-Oct-13 8:59am    
Hi ehsan..thanks for your answer.but I really dont understand what the code does...where exactly I am to call this function and how does the code actually check that a child form is closed and then assign it a null value?
[no name] 11-Oct-13 11:24am    
I am sorry i could not explain. ok I will try again,
As this function will close all the open mdichild forms so when ever you are going to open a new child form you will call this function and then open the new one.
I would recommend to read Sergey's answers about using MDI[^].

Sergey Alexandrovich Kryukov wrote:
Here is the best way of using MDI child: never using MDI.

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
 
Share this answer
 
C#
class ParentForm : Form {
    frmWebLeads formWeblead = null;

    //...

    private void leadsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if(formWeblead != null) return;
        formWeblead = new frmWebLeads();
        formWeblead.MdiParent = this;
        formWeblead.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        formWeblead.Show();

    }

}
 
Share this answer
 
Comments
Uzoma Umekwe 11-Oct-13 9:49am    
Hi Vishnu..your code is no much different from what I already have.what i want to achieve is to assign the child form to null after it closes...
Alright guys..Thanks to all of you for your help and ideas..Well I was able to sort this after several hours of head-banging :)

C#
private void connectToSourceDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
      {
          if (src_mdi_child == null || src_mdi_child.IsDisposed)
          {
              src_mdi_child = new ConnectToSource();
              src_mdi_child.MdiParent = this;
              src_mdi_child.Show();
          }
      }


So I added the IsDisposed property. The condition checks if the childform is null and if it has been previously disposed before opening the form. Tada! it works!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900