Detect from where an event is called - C #


I have an FormClosing event which is invoked by pressing the closing X of the window and also when I execute the this.Close() command.

The issue is that I want to detect which of the 2 forms this event was invoked.

Event code.

private void MDIPrincipal_FormClosing(object sender, FormClosingEventArgs e)
    if (MessageBox.Show("¿Desea salir del programa?", "Salir del Programa",
     MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
    else e.Cancel = true;

I use this code to ask me Do you want to exit the program ?, before closing the form. But I do not want you to ask me when using this.Close() Does anyone have any idea how to do it?

More Details .

In case it can not be detected from where it was invoked, an alternative solution occurred to me. Use an extra string detonador parameter.

For example.

private void MDIPrincipal_FormClosing(object sender, FormClosingEventArgs e, string detonador = "")
    if (detonador != "")
        //Código aquí...

But I do not know how to implement it. Or also deactivating this event so that it is not invoked.



The point is that I want the form to be closed without you asking me when using this.Close() .

asked by Jesse R. Jose 16.07.2017 в 15:50

1 answer


You can use this code.

         FormClosing -= MDIParent1_FormClosing;

Where + = NameForm_FormClosing; es para activar el evento y - = NameForm_FormClosing; ' to deactivate it.

In your case you would put it in the event Click of the button to close session

answered by 16.07.2017 / 18:48