/// <summary>
        /// Event delegate method fired when the <see cref="PasswordDialog"/> form is closing.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void PasswordDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Cancel)
            {
                _passwordFlags.Cancelled = true;
                return;
            }

            if (PasswordExpiredDialog)
            {
                // Check if the new password and its confirmation match, otherwise notify the user and exit.
                if (NewPasswordTextBox.Text != ConfirmTextBox.Text)
                {
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Properties.Resources.PasswordResetErrorTitleText, Properties.Resources.PasswordsMismatchErrorText));
                    e.Cancel = true;
                    return;
                }

                // Reset the password and if the reset is successful assign the new password to the local connection.
                _wbConnection.Password = PasswordTextBox.Text;
                try
                {
                    _wbConnection.ResetPassword(ConfirmTextBox.Text);
                }
                catch (Exception ex)
                {
                    MySqlSourceTrace.WriteAppErrorToLog(ex);
                    InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Properties.Resources.PasswordResetErrorTitleText, Properties.Resources.PasswordResetErrorDetailText));
                    _passwordFlags.Cancelled = true;
                    return;
                }

                _wbConnection.Password = ConfirmTextBox.Text;
            }
            else
            {
                _wbConnection.Password = PasswordTextBox.Text;
            }

            _passwordFlags.NewPassword = _wbConnection.Password;
            bool connectionSuccessful = false;

            if (_testConnection)
            {
                // Test the connection and if not successful revert the password to the one before the dialog was shown to the user.
                TestConnectionResult connectionResult = _wbConnection.TestConnectionAndReturnResult(false);
                _passwordFlags.ConnectionResult = connectionResult;
                switch (connectionResult)
                {
                case TestConnectionResult.ConnectionSuccess:
                case TestConnectionResult.PasswordReset:
                    connectionSuccessful = true;

                    // If the pasword was reset within the connection test, then set it again in the new password flag.
                    if (connectionResult == TestConnectionResult.PasswordReset)
                    {
                        _passwordFlags.NewPassword = _wbConnection.Password;
                    }

                    break;

                case TestConnectionResult.PasswordExpired:
                    // This status is set if the password was expired, and the dialog shown to the user to reset the password was cancelled, so exit.
                    return;
                }
            }

            // If the connection was successful and the user chose to store the password, save it in the password vault.
            if (!StorePasswordSecurely || !connectionSuccessful || string.IsNullOrEmpty(_wbConnection.Password))
            {
                return;
            }

            string storedPassword = MySqlWorkbenchPasswordVault.FindPassword(_wbConnection.HostIdentifier, _wbConnection.UserName);

            if (storedPassword == null || storedPassword != _wbConnection.Password)
            {
                MySqlWorkbenchPasswordVault.StorePassword(_wbConnection.HostIdentifier, _wbConnection.UserName, _wbConnection.Password);
            }
        }