public bool AddNew() { if (Save()) { if (_record != null) { _priorRecord = _record; } _record = new TRecord(); _suspend = true; foreach (var action in _setDefaults) { action.SetControl.Invoke(); if (action.InvokeSetProperty) { action.SetProperty.Invoke(_record); } } NewRecord?.Invoke(this, new EventArgs()); FirstControl?.Focus(); ValidationPanel?.SetStatus(RecordStatus.Valid, "New record started"); ToolStrip?.OnNew(); _suspend = false; return(true); } return(false); }
public bool Load(TRecord record) { if (record == null) { throw new ArgumentNullException(nameof(record)); } if (Save()) { if (_record != null) { _priorRecord = _record; } _record = record; _suspend = true; foreach (var action in _setControls) { action.Invoke(record); } RecordLoaded?.Invoke(this, new EventArgs()); ValidationPanel?.SetStatus(RecordStatus.Valid, "Record loaded"); ToolStrip?.OnClean(false); _suspend = false; return(true); } return(false); }
public bool Validate() { using (var cn = _db.GetConnection()) { cn.Open(); string message; RecordStatus status = (_record.IsValid(cn, out message)) ? RecordStatus.Editing : RecordStatus.Invalid; ValidationPanel?.SetStatus(status, message ?? "Record has no errors"); return(status == RecordStatus.Valid); } }
public bool Save() { // get the textboxes with changes that haven't fired Validated event var unvalidatedTextboxes = _textChanges.Where(kp => kp.Value && !_validated[kp.Key]).Select(kp => kp.Key).ToList(); foreach (var tb in unvalidatedTextboxes) { _textBoxValidators[tb].Validated.Invoke(this, new EventArgs()); } if (_suspend) { return(true); } _suspend = true; try { if (IsDirty) { SavingRecord?.Invoke(this, new EventArgs()); _db.Save(_record); IsDirty = false; RecordSaved?.Invoke(this, new EventArgs()); ValidationPanel?.SetStatus(RecordStatus.Valid, "Record saved"); } return(true); } catch (Exception exc) { ValidationPanel?.SetStatus(RecordStatus.Invalid, exc.Message); if (MessageBox.Show($"The record could not be saved: {exc.Message}\r\nClick OK to try again or Cancel to lose your changes.", "Save Error", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { UndoChanges(); return(true); } return(false); } finally { _suspend = false; } }