Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. close and reopen child forms

close and reopen child forms

Scheduled Pinned Locked Moved C#
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faladrim
    wrote on last edited by
    #1

    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 } } }

    G 1 Reply Last reply
    0
    • F faladrim

      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 } } }

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use the Hide method to hide the window. Once you close it you can't show it any more.

      --- b { font-weight: normal; }

      F 1 Reply Last reply
      0
      • G Guffa

        Use the Hide method to hide the window. Once you close it you can't show it any more.

        --- b { font-weight: normal; }

        F Offline
        F Offline
        faladrim
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • F faladrim

          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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Why do you create the child forms in the constructor at all? Why don't just create them when you need them? Then you don't need the extra code to handle the case when they exist already.

          --- b { font-weight: normal; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups