c# - How to close the main form without letting the application close?

后端 未结 3 1463
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 06:59

What i want to do:
->Open a MDI form
->Close the projects main form
->Without closing the application

What i have done:

frmMain          


        
3条回答
  •  死守一世寂寞
    2021-01-16 07:32

    If you actually wish to close the main form, but leave the application running, you can make your default class launch the application context:

    using(MyContext myContext = new MyContext())
    {
        Applicaton.Run(myContext);
    }
    

    where MyContext inherits from ApplicatonContext.

    Your application context can then load and close forms as needed. I do this with one project where I have the context launch a login form. Only after it is completed successfully does the main form get loaded. I also allow the "logging out" from the program to be handled by terminating the main form and reloading the login form.

    EDIT: From the comments, it may be that all you are looking for is .Hide(). This doesn't cause your form to unload and a call to .Show() will restore it (for example, you can use a timer or another form holding a reference to your main form to make the call). I prefer to use an ApplicationContext because it takes the responsibility for keeping your program in memory away from a UI element which the user may attempt to close.

提交回复
热议问题