Forms
-
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
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 theClose()
method for it. Edit your Form2 constructor toprivate 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:
-
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
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