locked
Help - Main Form Closing whole Application - C#

    Question

  •  

    I know how to do this in VB, but in C# I do not.
    When I cancel the main form, lets say the loginform for example. It'll close all the other forms, how do I correct this?

    Saturday, April 26, 2008 9:46 AM

Answers

  • You need something like this in your Main method:
    Code Snippet

    static void main()

    {

       LoginForm lgf = new LoginForm();

       if ( lgf.ShowDialog() == DialogResult.OK )

    {

       Application.Run(new MainForm());

    }

    }


    You should design your LoginForm so that its DialogResult is equal to DialogResult.OK only when log in succedds.

    Another way is to show log in form in the Load event of your main form and close t if log in fails.
    Saturday, April 26, 2008 5:27 PM

All replies

  • Either show login form before you call Application.Run() to show the main form of your application or use this article to achieve the functionality you need: Use the ApplicationContext Class to Fully Encapsulate Splash Screen Functionality
    • Proposed as answer by JohnGrove Saturday, January 31, 2009 3:42 PM
    Saturday, April 26, 2008 10:35 AM
  • you would need a main form, which shows the login form.... if the main form is closed, then the application is closed, so be sure to show the other forms in a Modal dialog until they close all open forms, they cannot close the main form.

     

     

     

    • Proposed as answer by JohnGrove Sunday, January 25, 2009 3:52 PM
    Saturday, April 26, 2008 11:51 AM
  • In VB theres a way to disable it so the mainform doesn't close the whole application, there must be a code or something for that in C# musn't there?
    Would It be a good idea if I added in "This.Hide();" where it says "Form.show();" in the Login code description so then it'll hide it, and a button in the Fire Manager (part my programme) which will close the login form and logs you out basically, and you can't close the fire manager without clicking log out. would that be an alternative? will the "This.Hide();" Hide the login from the computer and make it invisible but its actually still there?

    Saturday, April 26, 2008 3:57 PM
  • no there isnt, as that was especially for VB.NET users.

     

    it isnt the best practice to hide and show forms all the time, because there might be a scenario where the app doesnt function as it should or you could forget the show a form and wondering why your app is still running in the background.

     

    as stated, make your main form dialog as a ModalDialog, then show the other forms infront. The app wont close until the user closes the Modal form, then they can get access to the main form

     

    Saturday, April 26, 2008 5:14 PM
  • You need something like this in your Main method:
    Code Snippet

    static void main()

    {

       LoginForm lgf = new LoginForm();

       if ( lgf.ShowDialog() == DialogResult.OK )

    {

       Application.Run(new MainForm());

    }

    }


    You should design your LoginForm so that its DialogResult is equal to DialogResult.OK only when log in succedds.

    Another way is to show log in form in the Load event of your main form and close t if log in fails.
    Saturday, April 26, 2008 5:27 PM
  • People are referring to this for future answers so I will provide my comment.

    I agree with ahmedilyas's suggestion. The login form should be shown from the main form. One advantage is that that supports logging out and loggin in again, or something such as that. Also it makes it easier to transfer information to/from the login form from/to the main form.

    If the login form depends on processing that occurs outside of the login form, yet the login form is done separate from the main form, then the re-login gets more complicated.


    Sam Hobbs; see my SimpleSamples.Info
    • Proposed as answer by JohnGrove Sunday, January 25, 2009 3:52 PM
    Sunday, January 25, 2009 6:14 AM
  • I agree with ahmedilya, and Simple Samples (even though the last post in this original thread is like 9 months old).  You should not keep a secondary form alive after the main form (entry point) is disposed of.  Why?  I cant say, but it just doesnt make sense.  I'm sure there are some unique examples of this, but for 99% of people out there, I dont think this is a best practice, and the application logic should be redesigned.
    Compensating what I don't know yet, with what I do know now.
    • Proposed as answer by JohnGrove Sunday, January 25, 2009 3:52 PM
    Sunday, January 25, 2009 10:29 AM
  • I agree with ahmedilya, and Simple Samples (even though the last post in this original thread is like 9 months old).  You should not keep a secondary form alive after the main form (entry point) is disposed of.  Why?  I cant say, but it just doesnt make sense.  I'm sure there are some unique examples of this, but for 99% of people out there, I dont think this is a best practice, and the application logic should be redesigned.

    I agree that I was too quick to make my comment. I think that one deficiency in Microsoft's samples and articles and the .Net classes is support of business logic for smaller applications such as this. A form should be more of a UI and less of a class for business logic. Also, at the time, I was not familir with the ApplicationContext class. So I think that Giorgi Dalakishvili's suggestion is also useful.

    The purpose of the ApplicationContext class is to keep various forms alive. In other words, the ApplicationContext class can be used to keep additional forms alive if the requirements require it.


    Sam Hobbs; see my SimpleSamples.Info
    Thursday, March 11, 2010 6:57 PM