-5

How can I detect the click event of the close (X) button at the top right corner of the control box of a form/window in TextBox_Leave Event fired before this? I don't want to know about CloseReason, FormClosing, FormClosed or stuffs like these, unless they are inevitable.

I exactly want to detect if the user clicked the X button of the form. My Case Clearly is that i have textBox and Button. In TextBox_Leave there is a new Screen opened but when textBox is focused then Press (X) Button the Leave event Fire firstly but i need to konw if the user Press (X) Button Or Not to Show new Screen From Leave Event Or Close Current Screen.

I Can not use the mentioned events because it will fire after TextBox_Leave But i wanna know if the (X) Button Pressed Before Execute TextBox_Leave Code.

Mariana Magdy
  • 11
  • 1
  • 3

3 Answers3

2

if I understand correctly try this:

bool isButtonClick = false;

private void button1_Click(object sender, EventArgs e)
{
    isButtonClick = true;
    this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if(isButtonClick)
    {
         //User close form with Button
    }
    else
    {
         //User close form with (X) button at the top right corner of the
         //control box of a form/window
    }
}
Sultan
  • 319
  • 3
  • 11
  • I've made ​​a mistake in some words in my Question ... So, My Case Clearly is that i have textBox and Button in TextBox_Leave there is a new Screen opened but when textBox is focused then Press (X) Button the Leave event Fire firstly but i need to konw if the user Press (X) Button Or Not to Show new Screen From Leave Event Or Close Current Screen. – Mariana Magdy May 19 '14 at 14:50
0

I am having a bit of a problem interpreting your question but if I understand correctly you would like to know if the user has opted to close the program before the user closes it so that you can stop any ongoing jobs?

Are there any reason to why you would not like to use form_closing for this or have I misunderstood your question entirely? since I would solve it using form_closing like this

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
            // Display a MsgBox asking the user to close the form.
            if (MessageBox.Show("Are you sure you want to close the form?", "Close Form",
               MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.No)
            {
                // Cancel the Closing event
                e.Cancel = true;
            }

}

The message box can of course be replaced with code to halt any ongoing jobs that are currently beeing executed by your code. But perhaps I misunderstood what you were looking for?

Karl-Henrik
  • 1,113
  • 1
  • 11
  • 17
  • I've made ​​a mistake in some words in my Question ... So, My Case Clearly is that i have textBox and Button in TextBox_Leave there is a new Screen opened but when textBox is focused then Press (X) Button the Leave event Fire firstly but i need to konw if the user Press (X) Button Or Not to Show new Screen From Leave Event Or Close Current Screen. I Can not Use Form1_FormClosing Because this event will fire after Leave event. – Mariana Magdy May 19 '14 at 14:51
0

The answer to your (edited) question is: "No". The TextBox.Leave event will fire first, then the processing of the close (X) button will occur.

However, we cannot give more information as to work with this without knowing why you want to flip the order.

John Arlen
  • 6,539
  • 2
  • 33
  • 42