/// <summary> /// Determine if it is okay to change this.DataSource, and if it is okay /// and the current record has unsaved changed then save that record. /// Is not okay if the current record is invalid. /// </summary> /// <returns></returns> public bool IsOkayToChangeDataSource(string operation) { if (mCurrentEntity != null) { if (mCurrentEntity.IsDirty) { ErrorList errors = new ErrorList(); SetCurrentFixedValuesAndValidate(errors); if (errors.MaxSeverity > ErrorSeverity.Warning) { ErrorDisplayForm.Show(errors); // Ask if want to discard changes. string msg = string.Format("If you {1} you will lose " + "edits to the current {0}. Do you wish to {1}?", DataSource.EntityDisplayName, operation); DialogResult result = MessageBox.Show(msg, "Close Form", MessageBoxButtons.YesNo); if (result != DialogResult.Yes) { return(false); } } else { // Save without asking. DataSource.Save(CurrentEntity); } } } return(true); }
/// <summary> /// Validate and persist the current entity when the form closes. /// Cannot persist an invalid entity, so asks the user whether they /// want to close form if the current entity is invalid. This is the /// only place this class performs validation. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormClosingHandler(object sender, FormClosingEventArgs e) { if (mCurrentEntity != null) { if (mCurrentEntity.IsDirty) { ErrorList errors = new ErrorList(); SetCurrentFixedValuesAndValidate(errors); if (errors.MaxSeverity > ErrorSeverity.Warning) { // Validation errors may be displayed before this event fires // in some scenarios (e.g. DataGridView.RowValidating event). if (ShowErrorsInFormClosing) { ErrorDisplayForm.Show(errors); } // Ask if want to discard changes. string msg = string.Format("If you close the form you will lose " + "edits to the current {0}. Do you wish to close the form?", DataSource.EntityDisplayName); DialogResult result = MessageBox.Show(msg, "Close Form", MessageBoxButtons.YesNo); // NOTE: Empirically, on entry c.Cancel==true if there was a validation // error in RowValidating, and c.Cancel==false if not. This suggests that // DataGridView adds its own FormClosing event handler and prevents the // form from closing if there is a validation error. e.Cancel = (result != DialogResult.Yes); } else { // Save without asking. DataSource.Save(CurrentEntity); } } } }
/// <summary> /// Used by subclasses to validate the current entity if there is one, /// and show errors if any found. /// </summary> /// <returns></returns> public bool CurrentEntityIsValid() { EndEdit(); ErrorList errors = new ErrorList(); if (mCurrentEntity != null) { SetCurrentFixedValuesAndValidate(errors); if (errors.MaxSeverity > ErrorSeverity.Warning) { ErrorDisplayForm.Show(errors); return(false); } } return(true); }
private void RowDeletingHandler(object sender, DataGridViewRowCancelEventArgs e) { EndEdit(); ErrorList errors = new ErrorList(); if (CurrentEntity != null && CurrentEntity.IsPersisted) { ValidateDeleting(errors); if (errors.MaxSeverity > ErrorSeverity.Warning) { ErrorDisplayForm.Show(errors); e.Cancel = true; return; } DialogResult dlgRes = MessageBox.Show("Are you sure you want to delete this row?", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dlgRes != DialogResult.OK) { e.Cancel = true; return; } } }
public static void Show(ErrorList errors) { ErrorDisplayForm frm = new ErrorDisplayForm(); frm.ShowInternal(errors); }