Closing a form before opening another

Hi,
I am trying to develop a simple application. I have a login Screen and on successful login the main screen.
Now when the login is a success, I want to close the login form and show the main form.

P.S.
1. login form is the startup form
2. I do not want frmLogin.Hide()
3. when I try frmLogin.Close() before frmMain.Show()..flags error (I guess this is logically correct, but then whats the work around )

Please suggest the way in which i can accomplish this.

Thanks in advance



Answer this question

Closing a form before opening another

  • winstonSmith

    Here is my solution that works for me:
    Normal application start:
    Application.Run(formMain);

    In the formMain Load event:
    I instance spash form and show it. Start some initialization like database provider, reading application settings.
    Hide splash form
    Instance formLogin and show it as dialog form. Login form updates some public logged user variable if login is ok or else nothing.
    After login form is closed i check if that variable is ok and if it's not i exit application
    If login is ok, i show splash screen, finish some other initializations and checks and
    At the end i close splash screen
    and set Focus to main form.

    If you don't need splash screen you can remove those steps.



  • Peter Nimmo

    Sorry about taking a bit to get back to you on this...

    Now then... as you described it is not really possible without doing that work at a higher level or keeping frmLogin around in memory until your program quits.

    When you call Application.Run() as you see in your Program.cs file, that form is given control of the application and when it closes Application.Run() will return and begin the process of shutting down the application (provided you’ve got nothing below it in Program.cs) and take with it any object that that form referenced, including the new form you were creating.

    If you are unwilling/unable to keep the form around then I would highly suggest trying to implement something like what I described above as they really are your only two options.



  • AydinCinaRli

    Thanks a lot Brendan, I get your point.
    But, still I was wondering is it possible through the other way. I mean if its too late to implement the way you've mentioned. Could there be a direct workaround


  • tornin2

    Thanks Brendan. I accpet you solutions as my answer
    One more thing if you could help me on - Can you think of making it possible through multi-threading (If at all it can be, i am trying to learn this)

    Hey Boban,

    I actually have a different senario. The login form was just an example.

    My question was specifically on a situation whereto i have loaded a form into memory and now i spawn up a new form form it and then remove the old form from memory. Ok let me put it this way -
    I have a frmMain, i get some detials from the user. Now I have to make a Web-service call to retirve data and so i need to show up the frmProgress. Now the data coming in is so huge that it takes aroung 30 mins to retirve. This data is to be finally shown on say excel sheet. So the problem i am having is to remove the unused frmMain from memory once its task is over. (Just consider this scenario to be true ).
    Thus what i actually want is, once the frmProgress is shown - Remove frmMain form memory.
    Now in this case, you solution would not hold logical..
    What i need is a general solution.

    Thanks a lot


  • vtortola

    Rather then try to open one form from another it might be worthwhile to consider having them both be at a sibling level of responsibility... that is to say you might try opening the login form from Program.cs and then if it has done the desired work and returns the expected result (DialogResult) then display the Main form, otherwise gripe to the user and quit or try again.

    This might look like:

    frmLogin login = new frmLogin();

    if(login.ShowDialog() == DialogResult.OK)
    {
    frmMain main = new frmMain();

    //Do any other setting up of main

    Application.Run(main);
    }
    else
    {
    MessageBox.Show("Invalid Credentials!");
    }



  • Closing a form before opening another