How to Know User Has Clicked "X" or the "Close" Button

How to know user has clicked X or the Close button?

Assuming you're asking for WinForms, you may use the FormClosing() event. The event FormClosing() is triggered any time a form is to get closed.

To detect if the user clicked either X or your CloseButton, you may get it through the sender object. Try to cast sender as a Button control, and verify perhaps for its name "CloseButton", for instance.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
if (string.Equals((sender as Button).Name, @"CloseButton"))
// Do something proper to CloseButton.
else
// Then assume that X has been clicked and act accordingly.
}

Otherwise, I have never ever needed to differentiate whether X or CloseButton was clicked, as I wanted to perform something specific on the FormClosing event, like closing all MdiChildren before closing the MDIContainerForm, or event checking for unsaved changes. Under these circumstances, we don't need, according to me, to differentiate from either buttons.

Closing by ALT+F4 will also trigger the FormClosing() event, as it sends a message to the Form that says to close. You may cancel the event by setting the

FormClosingEventArgs.Cancel = true. 

In our example, this would translate to be

e.Cancel = true.

Notice the difference between the FormClosing() and the FormClosed() events.

FormClosing occurs when the form received the message to be closed, and verify whether it has something to do before it is closed.

FormClosed occurs when the form is actually closed, so after it is closed.

Does this help?

How to return a value when the Close button [X] is clicked using the MessageBox? (C#)

it seems like we can't return a value if the user clicked on close[X] button, but we can simply add extra else statement since that would be the last option after constraining all the other buttons in the if statements.

Credits for answer to @preciousbetine and @SLaks

How to distinguish 'Window close button clicked (X)' vs. window.Close() in closing handler

I'm not sure I like this at all but it's a question that you obviously have a reason for asking. if you were to take a stack trace in the OnClosing event you could look up for the Window.Close event.

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
bool wasCodeClosed = new StackTrace().GetFrames().FirstOrDefault(x => x.GetMethod() == typeof(Window).GetMethod("Close")) != null;
if (wasCodeClosed)
{
// Closed with this.Close()
}
else
{
// Closed some other way.
}

base.OnClosing(e);
}

vb.net: Distinguish between me.close() and x-button

The class FormClosingEventArgs event args has an enumeration to tell what the reason the form is closing.

https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason(v=vs.110).aspx

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.Closing

If Not appClosing AndAlso e.CloseReason = System.Windows.Forms.CloseReason.UserClosing Then

' DO WHATEVER CODE YOU WANT IN HERE
' LIKE SETTING E.CANCEL TO TRUE

End If

End Sub

' You can also set a close appClosing local variable at the class level as a boolean, whenever you call closing in code, assign the variable.
appClosing = True
Me.Close();


Related Topics



Leave a reply



Submit