private void frmStart_Load(object sender, EventArgs e) { intFoodstuffCount = DataConnection.NumFoodstuffs(); intRecipeMaterialCount = DataConnection.NumRecipeMaterials(); }
private void btnEditRecipe_Click(object sender, EventArgs e) { if (btnEditRecipe.Text == "Edit Recipe") { //Switches the form into a editable state txtName.Enabled = true; txtPriceSold.Enabled = true; txtPrepTime.Enabled = true; txtCookTime.Enabled = true; txtDirections.Enabled = true; txtDirections.ReadOnly = false; txtTags.Enabled = true; txtServingSize.Enabled = true; cboIng.Enabled = true; cboUnit.Enabled = true; txtQty.Enabled = true; btnAddIng.Enabled = true; btnEditIng.Enabled = true; btnDeleteIng.Enabled = true; btnAddToTagList.Enabled = true; btnRemoveSelectedTag.Enabled = true; btnEditRecipe.Text = "Save Updates"; lsvIngredients.Enabled = true; lbxTags.Enabled = true; btnPrint.Visible = true; } else { //Save Data...Hunter's version of what dustin did on the Entry page DialogResult button = MessageBox.Show("Are you sure you want to overwrite the previous data?", "Save Edited Recipe", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (button == DialogResult.Yes) { //Basically, create brand new foodstuff from all the parameters here, getting the old FSID //then remove the old foodstuff's recipematerial entries that don't match the new one's string strID = fstoUpdate.ID; string strName = fstoUpdate.Name; if (txtName.Text != strName) { strName = txtName.Text; } int intPrepTime = fstoUpdate.PrepTime; if (txtPrepTime.Text == "") { txtPrepTime.Text = "-1"; } else if (txtPrepTime.Text != intPrepTime.ToString()) { if (!int.TryParse(txtPrepTime.Text, out intPrepTime)) { MessageBox.Show("Error converting new prep time value. Did you enter it correctly?"); return; } } int intCookTime = fstoUpdate.CookTime; if (txtCookTime.Text == "") { txtCookTime.Text = "-1"; } else if (txtCookTime.Text != intCookTime.ToString()) { if (!int.TryParse(txtCookTime.Text, out intCookTime)) { MessageBox.Show("Error converting new cook time value. Did you enter it correctly?"); return; } } int intServings = fstoUpdate.Servings; if (txtServingSize.Text == "") { txtServingSize.Text = "-1"; } else if (txtServingSize.Text != intServings.ToString()) { if (!int.TryParse(txtServingSize.Text, out intServings)) { MessageBox.Show("Error converting new serving size value. Did you enter it correctly?"); return; } } double dblCost = fstoUpdate.Cost; if (txtPriceSold.Text == "") { txtPriceSold.Text = "-1.0"; } else if (txtPriceSold.Text != dblCost.ToString()) { if (!Double.TryParse(txtPriceSold.Text, out dblCost)) { MessageBox.Show("Error converting new price value. Did you enter it correctly?"); return; } } string strDirections = fstoUpdate.Directions; if (txtDirections.Text == "") { strDirections = txtDirections.Text; } else if (txtDirections.Text != strDirections) { strDirections = txtDirections.Text; } List <string> newTags = new List <string>(); if (lbxTags.Items.Count > 0) { for (int i = 0; i < lbxTags.Items.Count; i++) { newTags.Add(lbxTags.Items[i].ToString()); } } // //if item in list but not fs's list, add it, then add as its own foodstuff //if item matches, check unit and quantity //if different, update from ingredient list //if item in fs's list is not in ingredient list, remove it List <Recipe> newIngrList = new List <Recipe>(); if (lsvIngredients.Items.Count > 0) { foreach (ListViewItem lvi in lsvIngredients.Items) { //check to see whether the item exists or not by matching name, which is the third column of the listview. if (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text).Count > 0) { //this is supposed to return true if a name match is found //and add it to the new food's ingredient list newIngrList.Add(new Recipe(strID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text)[0].ID), lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } else { //Did not find a match... //making dummy entries //grab the first up to 4 characters of the name string newPHID = lvi.SubItems[2].Text.Substring(0, Math.Min(lvi.SubItems[2].Text.Split(' ')[0].Length, 4)); newPHID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#"); //new placeholder ID, name from listview's name column FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[2].Text); //should automatically generate the self-referencing RecipeMaterial stub... DataConnection.AddFoodStuff(fsPlaceholder); //then put this in the list for the actual food we're going to add newIngrList.Add(new Recipe(strID, newPHID, lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } } } //now update it! DataConnection.UpdateFoodstuff(new FoodStuff(strID, strName, strDirections, intPrepTime, intCookTime, dblCost, intServings, newTags, newIngrList)); } //List<Recipe> newItemIngredients = new List<Recipe>(); //if (lsvIngredients.Items.Count == 0) //{ // //Not sure if this will need to change for updating...We only need the new ingredients // newItemIngredients.Add(new Recipe(newFS.ID, fstoUpdate.ID)); //} //else //{ // foreach(ListViewItem lvi in lsvIngredients.Items) // if(DataConnection.FindFoodstuffsNamed(lvi.SubItems[0].Text).Count > 0 ) // { // newItemIngredients.Add(new Recipe(fstoUpdate.ID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[0].Text)[0].ID), // lvi.SubItems[1].Text, lvi.SubItems[2].Text)); // } // else // { // //Did not find a match...Dustin Dummy entries // string newPHID = lvi.SubItems[0].Text.Substring(0, Math.Min(lvi.SubItems[0].Text.Split(' ')[0].Length, 4));//grab the first up to 4 characters of the name // newPHID += (DataConnection.NumFoodstuffs()+1).ToString("0000#"); // FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[0].Text); // DataConnection.AddFoodStuff(fsPlaceholder, new Recipe(newPHID, newPHID)); // newItemIngredients.Add(new Recipe(newFS.ID, newPHID, lvi.SubItems[1].Text, lvi.SubItems[2].Text)); // } //} //then reset the form back into a readonly state btnEditRecipe.Text = "Edit Recipe"; txtName.Enabled = false; txtPriceSold.Enabled = false; txtPrepTime.Enabled = false; txtCookTime.Enabled = false; txtDirections.Enabled = false; txtTags.Enabled = false; txtServingSize.Enabled = false; cboIng.Enabled = false; cboUnit.Enabled = false; txtQty.Enabled = false; btnAddIng.Enabled = false; btnEditIng.Enabled = false; btnDeleteIng.Enabled = false; btnAddToTagList.Enabled = false; btnRemoveSelectedTag.Enabled = false; txtDirections.ReadOnly = true; lblMessage.Text = "Data edit saved"; timer1.Start(); lsvIngredients.Enabled = false; lbxTags.Enabled = false; } }
private void btnSaveRecipe_Click(object sender, EventArgs e) { /* * Here's what this is needing to do: * Validate the various fields on the form: * Name must exist * Rest are optional but if they do exist: * PriceSold is a double * ServingSize, PrepTime, CookTime are integers * Check if entries in the ingredients list (which are matched to foodstuffs) exist * in the foodstuff master list. * If they do exist, get their IDs and generate Recipe items to link this new item to them. * If they do NOT exist, prompt user whether to make generic entries for them * (i.e. ID and Name, rest being null) * Then make Recipe entries from these new foodstuffs we just made. * Once that's done, we can push to database...I think... * -Dustin */ if (blnValidate()) //really this is the level you'd do validation at { DialogResult button = MessageBox.Show("Are you sure you want to save this data?", "Save Recipe", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (button == DialogResult.Yes) { //they clicked yes, go about adding it //generate the id string newID = txtName.Text.Substring(0, Math.Min(txtName.Text.Split(' ')[0].Length, 4));//grab the first up to 4 characters of the name //get the item count and add one. newID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#"); List <string> newTags = new List <String>(); if (lbxTags.Items.Count > 0) { for (int i = 0; i < lbxTags.Items.Count; i++) { newTags.Add(lbxTags.Items[i].ToString()); } } //temp fix for blank textboxes so the foodstuff constructor doesn't error out with a blank string if (txtPrepTime.Text == "") { //convert to a double txtPrepTime.Text = "-1"; } if (txtCookTime.Text == "") { txtCookTime.Text = "-1"; } if (txtServingSize.Text == "") { txtServingSize.Text = "-1"; } if (txtPriceSold.Text == "") { txtPriceSold.Text = "-1.0"; } List <Recipe> newItemIngredients = new List <Recipe>(); //if lsvIngredients is empty...atomic ingredient? Don't hand the list to the constructor. if (lsvIngredients.Items.Count > 0) { foreach (ListViewItem lvi in lsvIngredients.Items) { //check to see whether the item exists or not by matching name, which is the third column of the listview. if (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text).Count > 0) { //this is supposed to return true if a name match is found //and add it to the new food's ingredient list newItemIngredients.Add(new Recipe(newID, (DataConnection.FindFoodstuffsNamed(lvi.SubItems[2].Text)[0].ID), lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } else { //Did not find a match... //making dummy entries //grab the first up to 4 characters of the name string newPHID = lvi.SubItems[2].Text.Substring(0, Math.Min(lvi.SubItems[2].Text.Split(' ')[0].Length, 4)); newPHID += (DataConnection.NumFoodstuffs() + 1).ToString("0000#"); //new placeholder ID, name from listview's name column FoodStuff fsPlaceholder = new FoodStuff(newPHID, lvi.SubItems[2].Text); //should automatically generate the self-referencing RecipeMaterial stub... DataConnection.AddFoodStuff(fsPlaceholder); //then put this in the list for the actual food we're going to add newItemIngredients.Add(new Recipe(newID, newPHID, lvi.SubItems[0].Text, lvi.SubItems[1].Text)); } } } //build the new foodstuff we're adding FoodStuff newFS = new FoodStuff(newID, txtName.Text, txtDirections.Text, Convert.ToInt32(txtPrepTime.Text), Convert.ToInt32(txtCookTime.Text), Convert.ToDouble(txtPriceSold.Text), Convert.ToInt32(txtServingSize.Text), newTags, newItemIngredients); //send to database DataConnection.AddFoodStuff(newFS); } //Then Clear the form and show a message stating the info was saved lblSaved.Text = "Recipe was saved. Please enter new recipe"; var cntrlCollections = GetAll(this, typeof(TextBox)); foreach (Control ctrl in cntrlCollections) { if (ctrl is TextBox) { ctrl.Text = string.Empty; } } lsvIngredients.Items.Clear(); lbxTags.Items.Clear(); timer1.Start(); } else { MessageBox.Show(ErrMessage); } // FoodStuff fs = new FoodStuff(txtName.Text, // if (blnValid = true) //{ // Add stuff into food stuff class and into database //} { } }