diff --git a/MVPLearning/BaseLibrary/BaseButton.cs b/MVPLearning/BaseLibrary/BaseButton.cs new file mode 100644 index 0000000..8c5f14c --- /dev/null +++ b/MVPLearning/BaseLibrary/BaseButton.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.BaseLibrary +{ + public class BaseButton : Button + { + } +} diff --git a/MVPLearning/BaseLibrary/BaseLabel.cs b/MVPLearning/BaseLibrary/BaseLabel.cs new file mode 100644 index 0000000..6a1071f --- /dev/null +++ b/MVPLearning/BaseLibrary/BaseLabel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.BaseLibrary +{ + public class BaseLabel : Label + { + } +} diff --git a/MVPLearning/MainView.Designer.cs b/MVPLearning/MainView.Designer.cs index 54b562f..f332429 100644 --- a/MVPLearning/MainView.Designer.cs +++ b/MVPLearning/MainView.Designer.cs @@ -38,6 +38,7 @@ bindToModelToolStripMenuItem = new ToolStripMenuItem(); bindToViewToolStripMenuItem = new ToolStripMenuItem(); bindToViewWithEqualityToolStripMenuItem = new ToolStripMenuItem(); + validateModelToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); SuspendLayout(); // @@ -88,6 +89,7 @@ // testAreaToolStripMenuItem // testAreaToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { bindToModelToolStripMenuItem, bindToViewToolStripMenuItem, bindToViewWithEqualityToolStripMenuItem }); + testAreaToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { bindToModelToolStripMenuItem, bindToViewToolStripMenuItem, validateModelToolStripMenuItem }); testAreaToolStripMenuItem.Name = "testAreaToolStripMenuItem"; testAreaToolStripMenuItem.Size = new Size(66, 20); testAreaToolStripMenuItem.Text = "Test Area"; @@ -113,6 +115,13 @@ bindToViewWithEqualityToolStripMenuItem.Text = "Bind To View with Equality"; bindToViewWithEqualityToolStripMenuItem.Click += BindToViewWithEqualityToolStripMenuItem_Click; // + // validateModelToolStripMenuItem + // + validateModelToolStripMenuItem.Name = "validateModelToolStripMenuItem"; + validateModelToolStripMenuItem.Size = new Size(180, 22); + validateModelToolStripMenuItem.Text = "Validate Model"; + validateModelToolStripMenuItem.Click += ValidateModelToolStripMenuItem_Click; + // // MainView // AutoScaleDimensions = new SizeF(7F, 15F); @@ -141,5 +150,6 @@ private ToolStripMenuItem bindToModelToolStripMenuItem; private ToolStripMenuItem bindToViewToolStripMenuItem; private ToolStripMenuItem bindToViewWithEqualityToolStripMenuItem; + private ToolStripMenuItem validateModelToolStripMenuItem; } } diff --git a/MVPLearning/MainView.cs b/MVPLearning/MainView.cs index 33a5e03..838b1d1 100644 --- a/MVPLearning/MainView.cs +++ b/MVPLearning/MainView.cs @@ -1,6 +1,7 @@ using MVPLearning.RecordKeeping.SermonFiler; using MVPLearning.TestingArea.ModelProperties; using MVPLearning.TestingArea.ViewModelEquality; +using MVPLearning.TestingArea.Validation; using MVPLearning.TestingArea.ViewProperties; namespace MVPLearning @@ -36,5 +37,10 @@ namespace MVPLearning { _ = new ViewModelEqualityPresenter(new ViewModelEqualityView() { MdiParent = this }); } + + private void ValidateModelToolStripMenuItem_Click(object sender, EventArgs e) + { + _ = new ValidateController(new ValidateForm() { MdiParent = this }); + } } } diff --git a/MVPLearning/RecordKeeping/SermonFiler/MaintainSermonFilerModel.cs b/MVPLearning/RecordKeeping/SermonFiler/MaintainSermonFilerModel.cs index 457a3df..7405685 100644 --- a/MVPLearning/RecordKeeping/SermonFiler/MaintainSermonFilerModel.cs +++ b/MVPLearning/RecordKeeping/SermonFiler/MaintainSermonFilerModel.cs @@ -22,10 +22,8 @@ namespace MVPLearning.RecordKeeping.SermonFiler private string _minister = string.Empty; public string Where { get => _where; set => SetProperty(ref _where, value); } private string _where = string.Empty; - public string Ref_No { get => _ref_No; set => SetProperty(ref _where, value); } -#pragma warning disable IDE0044 // Add readonly modifier + public string Ref_No { get => _ref_No; set => SetProperty(ref _ref_No, value); } private string _ref_No = string.Empty; -#pragma warning restore IDE0044 // Add readonly modifier public string Notes { get => _notes; set => SetProperty(ref _notes, value); } private string _notes = string.Empty; public string Filename { get => _filename; set => SetProperty(ref _filename, value); } diff --git a/MVPLearning/TestingArea/AreaStrings.cs b/MVPLearning/TestingArea/AreaStrings.cs new file mode 100644 index 0000000..a262a5b --- /dev/null +++ b/MVPLearning/TestingArea/AreaStrings.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.TestingArea +{ + static class AreaStrings + { + public static string LastNameEmpty {get; }= "Last name cannot be empty."; + } +} diff --git a/MVPLearning/TestingArea/Validation/IValidateForm.cs b/MVPLearning/TestingArea/Validation/IValidateForm.cs new file mode 100644 index 0000000..159d0e2 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/IValidateForm.cs @@ -0,0 +1,11 @@ + +namespace MVPLearning.TestingArea.Validation +{ + public interface IValidateForm + { + event EventHandler? Save; + + void Invalid(ValidateInvalid model); + void LoadForm(ValidationModel model); + } +} \ No newline at end of file diff --git a/MVPLearning/TestingArea/Validation/ValidateController.cs b/MVPLearning/TestingArea/Validation/ValidateController.cs new file mode 100644 index 0000000..8e09d1f --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidateController.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.TestingArea.Validation +{ + public class ValidateController + { + private readonly IValidateForm _form; + + public ValidateController(IValidateForm form) { + _form = form; + _form.Save += Save; + _form.LoadForm(new() { Id = 0, Firstname = "John", Lastname = "Doe" }); + if (_form is Form window) { window.Show(); } + } + + private void Save(object? sender, ValidationModel e) + { + if (e == null || string.IsNullOrEmpty(e.Lastname)) { _form.Invalid(new()); return; } + + ValidationModel model = new() { Firstname = e.Firstname, Lastname = e.Lastname, Id = e.Id }; + _form.LoadForm(model); + } + } +} diff --git a/MVPLearning/TestingArea/Validation/ValidateForm.Designer.cs b/MVPLearning/TestingArea/Validation/ValidateForm.Designer.cs new file mode 100644 index 0000000..99b4917 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidateForm.Designer.cs @@ -0,0 +1,121 @@ +namespace MVPLearning.TestingArea.Validation +{ + partial class ValidateForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + lblFirstname = new BaseLibrary.BaseLabel(); + txtFirstname = new BaseLibrary.BaseTextBox(); + txtLastname = new BaseLibrary.BaseTextBox(); + lblLastname = new BaseLibrary.BaseLabel(); + btnSave = new BaseLibrary.BaseButton(); + baseButton1 = new BaseLibrary.BaseButton(); + SuspendLayout(); + // + // lblFirstname + // + lblFirstname.AutoSize = true; + lblFirstname.Location = new Point(12, 9); + lblFirstname.Name = "lblFirstname"; + lblFirstname.Size = new Size(65, 15); + lblFirstname.TabIndex = 0; + lblFirstname.Text = "First name:"; + // + // txtFirstname + // + txtFirstname.BorderStyle = BorderStyle.FixedSingle; + txtFirstname.Location = new Point(83, 7); + txtFirstname.MaxLength = 20; + txtFirstname.Name = "txtFirstname"; + txtFirstname.Size = new Size(155, 23); + txtFirstname.TabIndex = 1; + // + // txtLastname + // + txtLastname.BorderStyle = BorderStyle.FixedSingle; + txtLastname.Location = new Point(83, 36); + txtLastname.MaxLength = 20; + txtLastname.Name = "txtLastname"; + txtLastname.Size = new Size(155, 23); + txtLastname.TabIndex = 3; + // + // lblLastname + // + lblLastname.AutoSize = true; + lblLastname.Location = new Point(12, 38); + lblLastname.Name = "lblLastname"; + lblLastname.Size = new Size(64, 15); + lblLastname.TabIndex = 2; + lblLastname.Text = "Last name:"; + // + // btnSave + // + btnSave.Location = new Point(163, 65); + btnSave.Name = "btnSave"; + btnSave.Size = new Size(75, 23); + btnSave.TabIndex = 4; + btnSave.Text = "Save"; + btnSave.UseVisualStyleBackColor = true; + btnSave.Click += Save_Click; + // + // baseButton1 + // + baseButton1.Location = new Point(83, 65); + baseButton1.Name = "baseButton1"; + baseButton1.Size = new Size(75, 23); + baseButton1.TabIndex = 5; + baseButton1.Text = "Test"; + baseButton1.UseVisualStyleBackColor = true; + baseButton1.Click += Test_Click; + // + // ValidateForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(331, 174); + Controls.Add(baseButton1); + Controls.Add(btnSave); + Controls.Add(txtLastname); + Controls.Add(lblLastname); + Controls.Add(txtFirstname); + Controls.Add(lblFirstname); + Name = "ValidateForm"; + Text = "ValidateForm"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private BaseLibrary.BaseLabel lblFirstname; + private BaseLibrary.BaseTextBox txtFirstname; + private BaseLibrary.BaseTextBox txtLastname; + private BaseLibrary.BaseLabel lblLastname; + private BaseLibrary.BaseButton btnSave; + private BaseLibrary.BaseButton baseButton1; + } +} \ No newline at end of file diff --git a/MVPLearning/TestingArea/Validation/ValidateForm.cs b/MVPLearning/TestingArea/Validation/ValidateForm.cs new file mode 100644 index 0000000..5b67113 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidateForm.cs @@ -0,0 +1,63 @@ +using MVPLearning.BaseLibrary; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace MVPLearning.TestingArea.Validation +{ + public partial class ValidateForm : BaseForm, IValidateForm + { + private readonly ValidationModel _model = new(); + private ValidationModel? _loadedModel; + public ValidateForm() + { + InitializeComponent(); + _model.PropertyChanged += ModelChanged; + txtFirstname.Bind(_model, nameof(_model.Firstname)); + txtLastname.Bind(_model, nameof(_model.Lastname)); + } + + private void ModelChanged(object? sender, PropertyChangedEventArgs e) + { + EnableSave(); + } + private void EnableSave() + { + bool enable = !_model.Equals(_loadedModel); + btnSave.Enabled = enable; + } + public event EventHandler? Save; + public void LoadForm(ValidationModel model) + { + _loadedModel = model; + _model.Firstname = model.Firstname; + _model.Lastname = model.Lastname; + _model.Id = model.Id; + EnableSave(); + } + public void Invalid(ValidateInvalid model) + { + if (model.Lastname != null) + { + txtLastname.Focus(); + MessageBox.Show(model.Lastname, Text); + } + } + + private void Save_Click(object sender, EventArgs e) + { + Save?.Invoke(this, _model); + } + + private void Test_Click(object sender, EventArgs e) + { + _model.Firstname = "Test"; + } + } +} diff --git a/MVPLearning/TestingArea/Validation/ValidateForm.resx b/MVPLearning/TestingArea/Validation/ValidateForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidateForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MVPLearning/TestingArea/Validation/ValidateInvalid.cs b/MVPLearning/TestingArea/Validation/ValidateInvalid.cs new file mode 100644 index 0000000..22a40c5 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidateInvalid.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.TestingArea.Validation +{ + public class ValidateInvalid + { + public string Lastname { get; set; } = AreaStrings.LastNameEmpty; + } +} diff --git a/MVPLearning/TestingArea/Validation/ValidationModel.cs b/MVPLearning/TestingArea/Validation/ValidationModel.cs new file mode 100644 index 0000000..bfb09b8 --- /dev/null +++ b/MVPLearning/TestingArea/Validation/ValidationModel.cs @@ -0,0 +1,32 @@ +using MVPLearning.Structure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MVPLearning.TestingArea.Validation +{ + public class ValidationModel : ObservableObject + { + private string _firstname = string.Empty; + private string _lastname = string.Empty; + private int _id; + + public string Firstname { get { return _firstname; } set { SetProperty(ref _firstname, value); } } + public string Lastname { get { return _lastname; } set { SetProperty(ref _lastname, value); } } + public int Id { get { return _id; } set { SetProperty(ref _id, value); } } + + public override bool Equals(object? obj) + { + return obj is ValidationModel model && + Id == model.Id && + Firstname == model.Firstname && + Lastname == model.Lastname; + } + public override int GetHashCode() + { + return base.GetHashCode(); + } + } +}