From f4b54e8c6fb209010ab38a466051491a7348cb5f Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Thu, 21 Jan 2021 22:58:52 -0800 Subject: [PATCH] Add: Basic functionality for the child crud form. --- frmAdminForm.cs | 31 ++++++++++++++++++++++--- frmChildCrud.cs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/frmAdminForm.cs b/frmAdminForm.cs index 15e8bb1..3bdbcb2 100644 --- a/frmAdminForm.cs +++ b/frmAdminForm.cs @@ -20,7 +20,7 @@ namespace GreatHomeChildcare { //globals for cheap access. SqliteDataAccess SqliteDataAccess = new SqliteDataAccess(); - int child_id = 0; + public static int child_id = 0; public frmAdminForm() { @@ -42,6 +42,8 @@ namespace GreatHomeChildcare */ private void RefreshAdminView() { + dgvChildren.Rows.Clear(); + List children = new List(); children = SqliteDataAccess.GetAllChildren(); @@ -67,14 +69,37 @@ namespace GreatHomeChildcare //TODO: new form to add a new child and their guardian(s) private void btnAdd_Click(object sender, EventArgs e) { - MessageBox.Show("Add button clicked."); + child_id = -1; //ENSURE!!!! + + ShowChildCrudForm(); } //TODO: new form(or same form as adding) for updating children //PB&J: get currently selected row from dgv, then pass to GetChildByID() to get Child object. private void btnUpdate_Click(object sender, EventArgs e) { - MessageBox.Show("Update button clicked."); + child_id = dgvChildren.CurrentRow.Index; + + //child_id must be incremented as the index starts at 0. + //TODO: FIX BUG!! + child_id++; + + ShowChildCrudForm(); + } + + private void ShowChildCrudForm() + { + Form frmCrud = new frmChildCrud(); + frmCrud.FormClosed += new FormClosedEventHandler(CrudFormClosed); + frmCrud.Show(); + Hide(); + } + + //Show this admin screen after the child crud form is closed. + private void CrudFormClosed(object sender, FormClosedEventArgs e) + { + RefreshAdminView(); + Show(); } private void btnQuit_Click(object sender, EventArgs e) diff --git a/frmChildCrud.cs b/frmChildCrud.cs index 6b393d2..9fef546 100644 --- a/frmChildCrud.cs +++ b/frmChildCrud.cs @@ -7,14 +7,76 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using GreatHomeChildcare.Models; namespace GreatHomeChildcare { public partial class frmChildCrud : Form { + const string DEFAULT_PIC_TAG = "DefaultPic"; + const string CUSTOM_PIC_TAG = "dickpic"; + + //Global instance of the SqliteDataAccess object. + SqliteDataAccess SqliteDataAccess = new SqliteDataAccess(); + + enum Gender + { + Female = 0, + Male = 1 + } + public frmChildCrud() { InitializeComponent(); } + + /* Event handler on form load. + * Reads the child_id from the admin form if the update button + * was selected on the admin form. + * INPUTS: child_id from the admin form + * OUTPUTS: void + */ + private void frmChildCrud_Load(object sender, EventArgs e) + { + int child_id = frmAdminForm.child_id; + + //If the update button was selected. + if (child_id > 0) + { + LoadChild(child_id); + } + } + + /* Load an existing child onto the form for update/delete operations. + * INPUT: integer child_id + * OUTPUT: data to screen + */ + private void LoadChild(int child_id_in) + { + Child child = SqliteDataAccess.GetChildByID(child_id_in); + + // sanity check, though it shouldn't be needed... + if (child == null) + return; + + //Load the child data into the form. + idNumericUpDown.Value = child.id; + firstNameTextBox.Text = child.FirstName; + lastNameTextBox.Text = child.LastName; + //TODO: Gender via enum + addressTextBox.Text = child.address; + photoPictureBox.Image = (child.photo != null) ? ImageWrangler.ByteArrayToImage(child.photo) : Properties.Resources.child; + LoadGuardiansForChild(child); + } + + private void LoadGuardiansForChild(Child child_in) + { + List guardians = SqliteDataAccess.GetGuardiansByChild(child_in); + + foreach (Guardian g in guardians) + { + dgvGuardians.Rows.Add(g.id, g.LastName, g.FirstName, g.PhoneNumber, g.EmailAddress); + } + } } }