Click to See Complete Forum and Search --> : How to close a form from the other one???
abelegreen
January 15th, 2009, 07:28 AM
Hi everybody!
I have one form(Form1) for login of user. After an user logined successfully, form2 will be appeared for handle other features. In form1, I used following code lines to open form2:
Form2 frm2 = new Form2();
frm2.Show();
this.Hide(); //this is form1
After that, form2 will be appeared. But form1 is still working too, but hidden. When I close form2, how to close form1 too :(
Thanks for any help.
MadHatter
January 15th, 2009, 07:32 AM
attach to form2's form closing event from inside form1, and call this.hide from inside form1.
Form2 frm2 = new Form2();
frm2.FormClosing += (s, e) => { Hide(); }; // .net 3.5 syntax
frm2.Show();
foamy
January 15th, 2009, 07:40 AM
I would do things differently. This is the Program.cs file:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Create instance of the login form.
Form1 f1 = new Form1();
//Show the form and evaluate the result when it returns.
if (f1.ShowDialog() == DialogResult.OK)
{
//If the result is good, start the application using the other form.
Application.Run(new Form2());
}
}
}
You will need to design Form1 to return the proper DialogResult value when validation is a success/fail.
It's probably not the best solution, but it should be a nice start :)
abelegreen
January 15th, 2009, 08:01 AM
Thanks all, I also found an another way to resolve this:
define a member variable in Form2 from Form1
//
private Form1 x;
//
then change Form2 Constructor in this case :
//
public Form2(Form1 x)
{
this.x = x;
}
//
Now in Form1 we have :
//we send form1(this) to Form2
private void button1_Click(object sender, System.EventArgs e)
{
Form2 x = new Form2(this);
x.ShowDialog();
}
//
now in form2 OnClosing :
//
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
x.Close();
}
eclipsed4utoo
January 15th, 2009, 04:50 PM
or you could do this...
Form1 f1 = (Form1)Application.OpenForms["Form1"];
f1.Close();
darwen
January 15th, 2009, 06:15 PM
I wouldn't do it that way abelegreen. It's illogical.
You normally have a controlling form (A) and a child form (B).
A runs, brings up B, B recieves input and then closes, returning control to form A. Form A gets the data from Form B, processes it and continues.
Form B doesn't (and shouldn't) directly interact with form A. In fact child forms should ideally know nothing of their parents.
Foamy's way is preferable as it follows a logical pattern.
It's like your walking down a path going from A->B->C->D.
You wouldn't expect step C to be dependent on step A. I.e. you wouldn't expect the sequence to be A->B->C->A->D.
Do you see what I mean ?
Darwen.
toraj58
January 15th, 2009, 07:23 PM
i think as a view point of security Foamy's Suggestion is also better.
BigEd781
January 15th, 2009, 08:34 PM
i think as a view point of security Foamy's Suggestion is also better.
Good point, I would give you rep if I could.
foamy
January 16th, 2009, 03:57 AM
Seems I was too modest in my earlier post :)
Thanks guys!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.