Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window close events in a winforms application

I am looking to prompt the user to save data when they close a form window in a winforms application. I can't figure out how to trigger the prompt to the user, should they click the red box at the top right corner of the form.

My application currently has a boolean flag, that is set to True on textchanged event. So I will only need to check for the boolean value in whatever event is trigger by the red box.

Any advice?

like image 309
user279521 Avatar asked Jan 31 '11 13:01

user279521


People also ask

How do I close a window in WinForms?

Form. Close() is one method in closing a winform. When 'Form. Close()' execute , all resources created in that form are destroyed.

Which of the following method is used to close the windows form application?

Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.

Is WinForms going away?

As we mentioned above, WinForms is still available but the status of “maintenance mode” likely means it has no long term future. As time passed by, especially in the last 5-10 years, new tools continued to mature and rise in popularity, and each one of them offered many powerful features.

Which event is triggered when a form is closed?

The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened.


3 Answers

You need to handle the FormClosing event. This event is raised just before the form is about to be closed, whether because the user clicked the "X" button in the title bar or through any other means.

Because the event is raised before the form is closed, it provides you with the opportunity to cancel the close event. You are passed an instance of the FormClosingEventArgs class in the e parameter. By setting the e.Cancel property to True, you can cancel a pending close event.

For example:

Private Sub Form_Closing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
    If Not isDataSaved Then
        ' The user has unsaved data, so prompt to save
        Dim retVal As DialogResult
        retVal = MessageBox.Show("Save Changes?", YesNoCancel)
        If retVal = DialogResult.Yes Then
            ' They chose to save, so save the changes
            ' ...
        ElseIf retVal = DialogResult.Cancel Then
            ' They chose to cancel, so cancel the form closing
            e.Cancel = True
        End If
        ' (Otherwise, we just fall through and let the form continue closing)
    End If
End Sub
like image 50
Cody Gray Avatar answered Oct 05 '22 03:10

Cody Gray


If you override the form's OnFormClosing method, you have the chance to notify the user that changes have been made, and the opportunity to cancel closing the form.

The event provides you with a FormClosingEventArgs instance which has a CloseReason property (which tells you why the form is closing) as well as a Cancel property, which you can set to True to stop the form from closing.

like image 28
stuartd Avatar answered Oct 05 '22 03:10

stuartd


I implent this code for C# hope so it useful to you

protected override void OnFormClosing(FormClosingEventArgs e)
            {            
                base.OnFormClosing(e);
                if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
                {
                    Dispose(true);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }

        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }
like image 41
IMMORTAL Avatar answered Oct 05 '22 04:10

IMMORTAL