How to open a new window form within another form in c#

sanzy picture sanzy · Jun 11, 2013 · Viewed 11.4k times · Source

i have developed a windows form application using c#.

it has 2 forms like login form and main form. When i enter the correct login credentials it should close(not hide) the login form and display the main form.

i used the following code

MainForm main=new MainForm();
this.hide();//close login form
main.show();//display main form

but when I close the main form using the cross mark(right upper corner) in the mdi container, the main form closes but the application is still running in the task manager.

If I use the following code instead of the previous code, application will close before main form display.

this.close()//close login form
main.show();//display main form

do i have to hide mdi container from the main form or is there any way to achieve this? please help.

Answer

Sathish picture Sathish · Jun 11, 2013

Try like this:

this.Hide();
Main.ShowDialog();
this.Close();

First, you hide the login form. Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed. Last, once the dialog is closed, you close the login form, ending the application.