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. Forms

Forms

Scheduled Pinned Locked Moved C#
3 Posts 3 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.
  • M Offline
    M Offline
    m m _2007
    wrote on last edited by
    #1

    i have two Forms : Form1 and Form2 from Form1 i show Form2: Form2 form2 = new Form2(); form2.Show(); i want from Form2 to Close Form1 by using the same code: Form1 form1 = new Form1(); form2.Show(); but doesn't close Form1

    N P 2 Replies Last reply
    0
    • M m m _2007

      i have two Forms : Form1 and Form2 from Form1 i show Form2: Form2 form2 = new Form2(); form2.Show(); i want from Form2 to Close Form1 by using the same code: Form1 form1 = new Form1(); form2.Show(); but doesn't close Form1

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello I don't know why someone flamed you with score 1.0, and didn't even answer you -wasn't me btw-. Your question is quite very simple -forgive me for saying that-. I guess you didn't do much reading in OOP did you?;) The first block of code is fine in creating a form, while the second is far from closing one:

      m.m._2007 wrote:

      Form1 form1 = new Form1();

      The above line creates a new form of type Form1. So refereing to it would be to the new form not your original form.

      m.m._2007 wrote:

      form2.Show();

      And this line shows the new form that you've just made in the previous block. Why on earth did you expect it to close the original one? Shouldn't you've called the method Close() at least? To make your code work you have to pass reference to your original form to the Form2 object and call the Close() method for it. Edit your Form2 constructor to

      private Form1 orgigenal
      Form2(Form1 or)
      {
      origenal = or;
      //More code
      }

      Now show Form2 like this:

      Form2 myForm = new Form2(this);// referencing to the original form

      and in Form2 close Form1 like this

      origenal.Close();

      I hope you got it, and I strongly suggest you do more reading in C# OOP programming. -- modified at 21:59 Tuesday 24th October, 2006

      Regards:rose:

      1 Reply Last reply
      0
      • M m m _2007

        i have two Forms : Form1 and Form2 from Form1 i show Form2: Form2 form2 = new Form2(); form2.Show(); i want from Form2 to Close Form1 by using the same code: Form1 form1 = new Form1(); form2.Show(); but doesn't close Form1

        P Offline
        P Offline
        phenix burn
        wrote on last edited by
        #3

        form2 is builded by form1 You close form1 in form2,and the application will terminate The code like this: Code in Form1: private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Frm = this; form2.Show(); } Code in Form2: /// /// Form which will close /// private Form _frm = null; public Form Frm { get { return _frm; } set { _frm = value; } } private void button1_Click(object sender, System.EventArgs e) { Frm.Close(); }

        phenix-burn

        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