Problem in creating MDI window forms
I have been try to create a window form in which when we open a new child form it checks if the child form is already open or not. If not open it create an new form but if a child form already exits it brings that form to front. I have tried using this code. It works fine for the first time but when i close the child form once & try to open it again it generates an exception saying, "Cannot use a disposed object.".
Pls someone help..
Thanks in advance...
Code:
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 TestProject
{
public partial class Form1 : Form
{
public static int flag1 = 0; // to check if form2 is already open or not...
public static int flag2 = 0; // to check if form3 is already open or not...
Form2 newform = new Form2();
Form3 newform1 = new Form3();
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag == 0)
{
flag = 1;
newform.MdiParent = this;
newform.Show();
}
else
{
newform.BringToFront();
}
}
private void new2ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (flag2 == 0)
{
flag2 = 1;
newform1.MdiParent = this;
newform1.Show();
}
else
{
newform1.BringToFront();
}
}
}
}