I made a C# Windows Forms application that requires the user to login using a form called Form1 as shown below:
// This code is called from Form1
Form2 f = new Form2();
f.Show();
// Then Form1 does some finalization logic and closes itself
After the user logs in, the login form should close and the main application window Form2 should be opened. But the problem I’m running into is that if I call Form2 from Form1, Form1 becomes Form2’s owner, thus closing Form1 closes both forms and ends the application.
How may I call Form2 such that it is independent of Form1?
The problem is not the owner, but that the Windows message loop is tied to Form1. When Form1 closes, so does the application. Look in your
Mainmethod:The easiest solution is to show your login form (assuming it’s a login form) as a modal dialog, and then begin the windows message loop on Form2:
EDIT:
Just did some googling and looks like another alternative is custom
ApplicationContexts. Never worked with these myself though:http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx