Ronni Marker

Hello Gents.
Am trying to alter the way ALT+F4 closes an application and hope I can get some guru help here.

I have a component that calls an Windows form for a regstration page. On that Windows form I have two buttons.

One closes the application by:

System.Environment.Exit();

and the other button closes the application by:

System.Enviroment.Exit(-1);

The first only closes the form that runs inside the component, the later closes the whole application including what ever application who calls the component.

Now my problem is that when the form that opens inside the compoent is active and a user hits ALT+F4, then the only the component closes and not the whole application.

To me it seems that a standard ALT+F4 is like invoking System.Environment.Exit() and not System.Enviroment.Exit(-1).

Is there any way I can force ALT+F4 to close the whole application like System.Enviroment.Exit(-1) does

Cheers,
Ronni


Re: Windows Forms General ALT + F4 (C# development)

Mario Cossi

Ronni,

as far as I know System.Environment.Exit () won't even compile, as the only overload for that method requires an int parameter. Might you be referring to Application.Exit () instead

Skipping the details, ALT+F4 and Application.Exit () are not the same in all conditions:

ALT+F4 is equivalent to clicking the close button: it closes the current window.

Application.Exit () closes all the windows of the application.

Yet, for an application with only one window, the two have pretty much the same effect.

You can solve the problem using the FormClosing event, and the CloseReason field of FormClosingEventArgs

private void form1_FormClosing (object sender, FormClosingEventArgs e) {

if (e.CloseReason != CloseReason.ApplicationExitCall) {

System.Environment.Exit (-1);

}

}

Remember that:

System.Environment.Exit (-1) will not raise the FormClosing event.

Application.Exit () will raise the FormClosing event.

You can be even more selective by inspecting the possible values in the CloseReason enumeration.

HTH

--mc





Re: Windows Forms General ALT + F4 (C# development)

Ronni Marker

Hi yea was alittle too fast there your right should have been Aplication.Exit()

Anyway thanks for the answer was just what I needed to finish this.

Cheers,

Ronni