Patrick vD
Patrick vD

Reputation: 779

Using Application.Close() from a Windows Form

I am making a Windows Form Application with several forms, and I want to be able to close the entire application from any of the Forms. My main problem is that the System.Windows.Forms.Application class, used in the Program.cs class is Private. I would like any of the forms to be able to use it directly with something like Program.Application.Exit();. I realize that I could just make a ApplicationExit method within the Program class, and just call that, but that means that I will need to make a new one if I want to use any other methods from the Application class.

This is what I have right now:

public static class Application
{
    public static void EnableVisualStyles()
    {
        System.Windows.Forms.Application.EnableVisualStyles();
    }
    public static void Run()
    {
        System.Windows.Forms.Application.Run();
    }
    public static void Exit()
    {
        System.Windows.Forms.Application.Exit();
    }
    public static void SetCompatibleTextRenderingDefault(bool defaultValue)
    {
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(defaultValue);
    }
}

But with this I need to add each of the methods individually, and if I want to be able to manage variables I need to start using a ton of {get; set;} statements. This works, but I would like to know if there is a way to just make a class called Application within the Program class that directly inherits or imports the System.Windows.Forms.Application class.

Upvotes: 0

Views: 515

Answers (1)

George Vovos
George Vovos

Reputation: 7618

What you're doing is correct

Application.Exit()

Terminates the entire application.
If you want to close only the form you must only call the Close() method of the Form class

Upvotes: 3

Related Questions