Mr.Rendezvous
Mr.Rendezvous

Reputation: 2003

How to close Main window or application from ShowDialog window

As per question. So I have a main window (MainForm) load when application start. Then in MainForm_load event it do showDialog another form (LoginForm.showDialog()).

What I struggling about is, how can if user close the LoginForm (the x sign in the top right corner) it close the application? in LoginForm there is only one button labeled "Login"

Thx before for the answer :)


well I've been trying this

    private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
    {

        if (e.CloseReason == CloseReason.UserClosing)
        {
            Application.Exit();

        }
        else
        {
            this.Dispose();
        }
    }

but it's invalid operation exception on Application.Exit()

so what I want is the application exit only if I close this Login form using [x]close button.

Upvotes: 0

Views: 1635

Answers (2)

Mr.Rendezvous
Mr.Rendezvous

Reputation: 2003

Well I shouldnt use the frmLogin_FormClosing event at the first.

I use frmLogin_FormClosed

    private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            Application.Exit();

        }
    }

thanks for all the help and attention guys :)

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942000

Do this in Program.cs, like this:

    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using (var login = new LoginForm()) {
            if (login.ShowDialog() != DialogResult.OK) return;
        }
        Application.Run(new MainForm());
    }

Upvotes: 2

Related Questions