I am having problems closing out a C# windows form app. It will currently just give me a blank form with no title or anything. I need to find a way to close this little unknown window.
I Have 2 form pages one for a login screen and one for the actual app. All being run by a program.cs file.
program.cs
... static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new PROG1()); }
This is just the basic main created by visual studio to run my program.
Then we have the main program
PROG1.cs
... public PROG1() { Login LoginForm = new Login(); DialogResult a = LoginForm.ShowDialog(); if(LoginForm.ValidLogin == 1) { InitializeComponent(); } else { Application.Exit(); //FAIL } }
You can see that the first the program.cs file calls PROG1.cs which calls an instance of login.cs. If you insert a valid login the login page will close and the main PROG1 will show as it should. However if you simply click the red X to close login form it comes to Prog1 and LoginForm.ValidLogin != 1 so it does not Initialize the form and will try to close the form. This will just leave me the uninitialized form instead of closing it out. If I do a this.close() instead it will give me a runtime error.
Any Ideas?
Put the InitializeComponent call back at the top where it used to be. An attempt to use uninitialized variables, including trying to tell them to close themselves, is a bad idea.
Configure the properties of your PROG1 form so the initial state will be hidden instead of shown. After the LoginForm returns, your PROG1 code can decide whether to show itself or close itself.