close and reopen child forms
-
hi, if i press a button in a menu in a parent form i open a child form called form2, if i choose form3 in the menu i want to close form2 and show form3, and visa versa, but i cant do this. i get an error because the form2 is closed and i cant reopen it. it has someting to doe with the signle instance... i dont understand it :( this i what i have of code, very simple it just opens empty forms: namespace swapform { public partial class Form1 : Form { Form2 objchild1 = new Form2(); Form3 objchild = new Form3(); public Form1() { InitializeComponent(); objchild1.MdiParent = this; objchild.MdiParent = this; } private void form3ToolStripMenuItem_Click(object sender, EventArgs e) { objchild.Show(); //show form3 objchild1.Close(); // close form 2 } private void form2ToolStripMenuItem_Click(object sender, EventArgs e) { objchild1.Show(); //show form 2 objchild.Close();//close form 3 } } }
-
hi, if i press a button in a menu in a parent form i open a child form called form2, if i choose form3 in the menu i want to close form2 and show form3, and visa versa, but i cant do this. i get an error because the form2 is closed and i cant reopen it. it has someting to doe with the signle instance... i dont understand it :( this i what i have of code, very simple it just opens empty forms: namespace swapform { public partial class Form1 : Form { Form2 objchild1 = new Form2(); Form3 objchild = new Form3(); public Form1() { InitializeComponent(); objchild1.MdiParent = this; objchild.MdiParent = this; } private void form3ToolStripMenuItem_Click(object sender, EventArgs e) { objchild.Show(); //show form3 objchild1.Close(); // close form 2 } private void form2ToolStripMenuItem_Click(object sender, EventArgs e) { objchild1.Show(); //show form 2 objchild.Close();//close form 3 } } }
-
Use the
Hide
method to hide the window. Once you close it you can't show it any more.--- b { font-weight: normal; }
i found a way, cause when i just hide the form is still open and i dont want that. here is the solution to be able to close a child form when opening another. namespace swapform { public partial class Form1 : Form { Form3 objchild = new Form3(); Form2 objchild1 = new Form2(); public Form1() { InitializeComponent(); objchild.MdiParent = this; objchild1.MdiParent = this; } private void form2ToolStripMenuItem_Click(object sender, EventArgs e) { objchild1.Close(); if ((objchild == null) | (objchild.IsDisposed)) { objchild = new Form3(); objchild.MdiParent = this; } objchild.Show(); // show form3 objchild.Top = 10;//specify the location objchild.Left = 10; objchild.Height = 550; objchild.Width =560; } private void form1ToolStripMenuItem_Click(object sender, EventArgs e) { objchild.Close(); if ((objchild1 == null) | (objchild1.IsDisposed)) { objchild1 = new Form2(); objchild1.MdiParent = this; } objchild1.Show(); // show form 2 objchild1.Top = 10;//specify the location objchild1.Left = 10; objchild1.Height = 550; objchild1.Width = 560; } } grz
-
i found a way, cause when i just hide the form is still open and i dont want that. here is the solution to be able to close a child form when opening another. namespace swapform { public partial class Form1 : Form { Form3 objchild = new Form3(); Form2 objchild1 = new Form2(); public Form1() { InitializeComponent(); objchild.MdiParent = this; objchild1.MdiParent = this; } private void form2ToolStripMenuItem_Click(object sender, EventArgs e) { objchild1.Close(); if ((objchild == null) | (objchild.IsDisposed)) { objchild = new Form3(); objchild.MdiParent = this; } objchild.Show(); // show form3 objchild.Top = 10;//specify the location objchild.Left = 10; objchild.Height = 550; objchild.Width =560; } private void form1ToolStripMenuItem_Click(object sender, EventArgs e) { objchild.Close(); if ((objchild1 == null) | (objchild1.IsDisposed)) { objchild1 = new Form2(); objchild1.MdiParent = this; } objchild1.Show(); // show form 2 objchild1.Top = 10;//specify the location objchild1.Left = 10; objchild1.Height = 550; objchild1.Width = 560; } } grz