c# - How can I close a login form and show the main form without my application closing? -


i have 2 forms in project (login , main).

what i'm trying accoomplish is, if login successful, must show main form , close login form.

i have method in login form closes login form when login successful. main form doesn't show.

public void showmain() {     if(auth()) // method returns true when user exists.     {                      var main = new main();         main.show();         this.close();     }     else     {         messagebox.show("invalid login details.");     }          } 

i tried hiding login form if login process successful. bothers me because know while program running login form still there too, should closed right?

what should right approach this? thanks...

the reason main form isn't showing because once close login form, application's message pump shut down, causes entire application exit. windows message loop tied login form because that's 1 have set startup form in project properties. in "program.cs" file, , you'll see responsible bit of code: application.run(new loginform()). check out documentation method here on msdn, explains in greater detail.

the best solution move code out of login form "program.cs" file. when program first starts, you'll create , show login form modal dialog (which runs on separate message loop , blocks execution of rest of code until closes). when login dialog closes, you'll check dialogresult property see if login successful. if was, can start main form using application.run (thus creating main message loop); otherwise, can exit application without showing form @ all. this:

static void main() {     loginform flogin = new loginform();     if (flogin.showdialog() == dialogresult.ok)     {         application.run(new mainform());     }     else     {         application.exit();     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -