closing the main form after loading another form
-
I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!
-
I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!
-
I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!
Every form you create acts just like a class would so you could just do.
MySecondForm form = new MySecondForm();
form.Show();
this.Dispose();The 'this' keyword refers to the object you're form is derived from including all of the custom objects and functions you have put in. You could also just say 'Dispose();' instead of 'this.Dispose();' but for keeping clarity it's better to use the this keyword. You could also say 'this.Hide();' but then it keeps that form in memory which when you're dealing with multiple forms this could eat up your memory pretty quickly. I just take my forms out of memory when I don't need them. ------------------ I'm naked under my clothes...
-
Every form you create acts just like a class would so you could just do.
MySecondForm form = new MySecondForm();
form.Show();
this.Dispose();The 'this' keyword refers to the object you're form is derived from including all of the custom objects and functions you have put in. You could also just say 'Dispose();' instead of 'this.Dispose();' but for keeping clarity it's better to use the this keyword. You could also say 'this.Hide();' but then it keeps that form in memory which when you're dealing with multiple forms this could eat up your memory pretty quickly. I just take my forms out of memory when I don't need them. ------------------ I'm naked under my clothes...
this.Dispose(); doesn't work for me. it still closes the whole application. the this.Hide works and altought I'm making a small program a slow computer is gonna run it so if you can recommend me of somthimg else that will allow me to save in memory usage please find me somthing that doesn't just hides the window but thanks anyway. Modyfied: I just realized I don't need to close the main form and that the this.Dispose works for my other forms. anyway thanks alot you've been a great help for me. see ya later
-
I need to go to another form in my project. I need that the main form will close when I press a button to go to the second form. I can't use Application.Exit because than the second form closes too. please help!!!!
If you're using VS.NET, your application entry point probably looks like this:
[STAThread]
static void Main()
{
Application.Run(new Form1());
}This means that 1) this call will not return until Form1 has closed 2) the application will terminate immediately after Form1 is closed. If you need to close Form1 have have the app keep plugging away, you can't rely on the VS.NET-generated code. Instead, you can write a class to handle showing the form(s) you need to show.
//Rediculously simple example
public class AppStarter{
public AppStarter(){
Form1 frm = new Form1();
frm.Show();
}
[STAThread]
public void Main(){
Application.Run(new AppStarter());
}
}Now the application will run as long as the instance of AppStarter created in
Main()
is alive. You need to remember to callApplication.Exit()
when your last form closes or the app will never die, even if there's nothing on screen because the lifetime of the app is no longer directly related to the lifetime of the form. Charlie if(!curlies){ return; } -
this.Dispose(); doesn't work for me. it still closes the whole application. the this.Hide works and altought I'm making a small program a slow computer is gonna run it so if you can recommend me of somthimg else that will allow me to save in memory usage please find me somthing that doesn't just hides the window but thanks anyway. Modyfied: I just realized I don't need to close the main form and that the this.Dispose works for my other forms. anyway thanks alot you've been a great help for me. see ya later
You can't dispose the main form because that's what the message pump (which
Application.Run
starts) waits for. If you dispose the main form, you effectively let the message pump know that it can quit and your entry point returns. When the entry point returns, the CLR unloads your application (if there are no running background threads). See Charlie's response for a good solution (no need to repeat it). Also, you might consider a wizard-like interface, even if it has no "Back" or "Previous" button. You load oneForm
which adds and removes instances of container classes (like thePanel
) onto its surface. This is how property sheets and wizards (which actually use the same interfaces and functions) work in Windows Explorer and other applications that use the shell-provided property sheet implementation.Microsoft MVP, Visual C# My Articles