form.close() is not working
Hi,
I am using visual studio team suite 2005 for developing a windows app in C#
I have 2 forms Form1 and Form2
I have a button control on Form1 called Bttn. In the click event of the button i am writing the following code so that i close the current Form i.e. Form1 and want to show Form2.
MY code::
Form1 f = new Form1();
f.Close();
Form2 r = new Form2();
r.Visible = true;
but with this i have 2 problems
1) Form1 is not closed and Form2 is open i.e. both forms are opened (f.close() is not working)
2) After running the app, when i close Form1 Strangely Form2 is also closed.
but however if i close Form2 only that form2 is closed as expected. I am not implementing any mdi forms concept.
Any help is appreciated
thanks
red
Scott LyslePosted Feb 8, 2007, 9:40 PM
To be able to close Form1, you will need to rewrite the Program.cs file's main function to do something like this (I commented out the default line that will be added by the IDE). In this format, the application is not dependent upon the continued existence of Form1.
static
void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
Form1 f = new Form1();
f.Show();
Application.Run();
}As for the use of visibility as a property used to show the form, replace that with 'Show' as indicated above. Your button click event handler ought to look something like this (and also use 'Dispose' instead of 'Close'):
private
void btnExit_Click(object sender, EventArgs e){
Form1 f = new Form1();
f.Text = "Form 2";
f.Show();
this.Dispose();
}Now, you have one last problem. If you close Form2, the application is still running and you won't have a nice way to exit the application. You will need to give yourself another button to exit the application instead of closing Form1 or Form2, the click event handler for this other button should call 'Application.Exit()'. If you intend to exit the application whenever Form2 is closed, you may want to consider adding an event handler for the Form2_FormClosed event and in the handler, call Application.Exit.
Having said this much, this is not a very sound way to handle an application because, you cannot account for the disposal for Form1 by calling for the application to exit; therefore, if the user did not click the button and create an instance of Form2, and they closed Form1, the application would still be running even though it no longer supplies an interface to the user and there is no longer a way to exit the application.