Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c# application with multiple forms

Options
  • 28-01-2009 3:55pm
    #1
    Registered Users Posts: 197 ✭✭


    Hi

    I am trying to learn c# and I am attempting an application with multiple forms.
    I have started with a login form which is opened from Program.main using Application.Run(new login()). It has a button to login and then display the main application form if the login is successful. I can get this to work but when I close the login form after displaying the main form the application exits, I presume because I am creating and displaying the main form from within it.
    Can someone give me some advice on how to structure an application like this, how do I stop the applcation from shutting down once the first form is closed.


    Thanks

    Dave


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Post the code you are using to display the main form and hide the login form. I'd imagine the problem is in there.


  • Registered Users Posts: 197 ✭✭cracker


    From program.main I had

    Application.Run(new LoginForm);

    When that opens I had a login button which

    //Checks user against database and then
    //Opens the main screen
    MainScreenForm mainScreen = new MainScreenForm();
    //Close login screen
    this.close();

    I understand then that it just returns to Program.main and runs to the end of the application

    I have changed it now so that the Program.main opens an MDI form and then pops up a login screen. Depending on the result of the login it then either puts a child form in the MDI form or not if login is invalid.
    Is there a better way to structure this kind of thing?

    Thanks

    Dave


  • Registered Users Posts: 1,998 ✭✭✭lynchie


    cracker wrote: »
    From program.main I had

    Application.Run(new LoginForm);

    When that opens I had a login button which

    //Checks user against database and then
    //Opens the main screen
    MainScreenForm mainScreen = new MainScreenForm();
    //Close login screen
    this.close();

    You only instantiated MainScreenForm.. where do u actually show it and attach it to the message queue...


    Your code should be something similar to the following
    LoginForm lf = new LoginForm();
    Application.Run(lf);
    
    if(lf.WasUserAuthorised)
    Application.Run(new MainScreenForm());
    


  • Registered Users Posts: 197 ✭✭cracker


    Sorry, I did have the instantiation and show steps in my code, just forgot to include it here. I was doing this from within the login form.

    If I do as you suggest then I think the application will also close once the user shuts down the main screen. I would have to keep calling Application.Run in Program.main and that would tie me down to opening them in a particular order.
    I am thinking of a scenario where the app starts at one screen (possibly the login but not neccessarily) and then the user can move between multiple forms in the app before deciding to exit

    That is why I tried the MDI which opens at the start and just displays the other screens as they are called and the application will continue to run until the controlling MDI is shutdown.

    But is there a way to start with a normal form and then keep closing and opening other forms. Do I need some kind of controlling class which keeps the application alive until the user decides to exit?


Advertisement