Hello,
I am currently designing a system using C# winform. However, there is a problem in opening and closing multiple winform. For example, when i run the application, a login form will appear on top of the main form. The main form will only be enabled after user enter the valid username and password. When user login with a valid username and password, the login form will be close and the main form will be enabled.

Since I run the application using login form. When I close the login form, the whole application will be closed. If I hide the form then the application will still be running eventhough I closed all the forms.

Does anyone know how to solve this problem or any better recommendation?
Thank you.

Recommended Answers

All 6 Replies

That's for sure, let the main form to be the start and in it constructor call the login form after valid login close it, the application won't close.

Thank you for the help. But there still exist problem. The application will still be closed when the main form is closed.

However, it can be solved using MDIParent and Child Form. But can it be done without using MDI. I mean the design concept similar to webform. When user click on a button, then it will open the selected form and close the previous form in which means opening one form at time.

Main form means when you close it that's mean you close the application.
1- MDI won't work in modal dialog mode.
2- User will go through the main form without looking to your login form.

what supposed to done is to run the main form you may hide it then run the login form after success login show the main form.

It sounds like maybe you have a couple things backwards.
Your Main Application should not be the login screen. Instead, create a Winform application, and add the Login WinForm to it. Then when the main application loads (Load Event) , create the Login form and show it modal, and get the result from the Login form's DialogResult.

Login lg = newLogin();
if (lg.ShowDialog() != DialogResult.OK)
  Close();
 
... Do your main Application stuff ...

You can do your vierification tasks inside your Login form or in the main form. If they hit the cancel button or exit any other way than the Accept button, set the Login form's DialogResult to Cancel. If they Click your accept button, then (optionally do checks) set the DialogResult to Ok to get back to the main application.
Let the Main form continue if the DialogResult is Ok, otherwise Close the main application.

--Jerry

this.Hide();   //hide login form
Form1 mainForm = new Form1;   
mainForm.ShowDialog();   //important to use ShowDialog(); so that
this.Close();                //login form closes only when mainForm closes.

This thread is closed (or should be)
Started in Oct 2007, and the thread starter has not closed this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.