private bool TryAdd() { string name = this.txtName.Text.Trim(); if (string.IsNullOrEmpty(name)) { MessageBox.Show("食材类别的名称不能为空!"); return(false); } IDictionary <Guid, IngredientCategory> ingredientDict = IngredientCategory.GetAll(); var result = from item in ingredientDict.Values where item.Name == this.newCatetory.Name select item; if (result.Count() > 0) { MessageBox.Show(string.Format("已存在名为【{0}】的食材类别!", name), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } for (int i = 0; i < this.lstCategory.Items.Count; i++) { var obj = this.lstCategory.Items[i] as IngredientCategory; obj.Priority = i; } ingredientDict.Add(this.newCatetory.Id, this.newCatetory); IngredientCategory.SaveDatabase(typeof(IngredientCategory).Name); return(true); }
private bool TryAdd() { for (int i = 0; i < this.lstCategory.Items.Count; i++) { var obj = this.lstCategory.Items[i] as IngredientCategory; obj.Priority = i; } IngredientCategory.SaveDatabase(typeof(IngredientCategory).Name); return(true); }
private void btnDelete_Click(object sender, EventArgs e) { var selectedItem = this.lstCategory.SelectedItem; if (selectedItem != null) { if (MessageBox.Show(string.Format("是否确认删除【{0}】类别?(这将同步删除使用此类别的食材和使用这些食材的菜品!)", selectedItem), "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { // 删除category var category = selectedItem as IngredientCategory; IDictionary <Guid, IngredientCategory> categoryDict = IngredientCategory.GetAll(); categoryDict.Remove(category.Id); // 删除关联到category的食材ingredient IDictionary <Guid, Ingredient> ingredientDict = Ingredient.GetAll(); var toBeDeletedIngredients = (from item in ingredientDict.Values where item.Category.Id == category.Id select item.Id).ToList(); foreach (var id in toBeDeletedIngredients) { ingredientDict.Remove(id); } // 删除关联到食材ingredient的菜品dish IDictionary <Guid, Dish> dishDict = Dish.GetAll(); var toBeDeletedDishes = new List <Dish>(); foreach (var dish in dishDict.Values) { foreach (var weightedIngredient in dish.WeightedIngredientList) { if (toBeDeletedIngredients.Contains(weightedIngredient.Ingredient.Id)) { toBeDeletedDishes.Add(dish); break; } } } foreach (var dish in toBeDeletedDishes) { dishDict.Remove(dish.Id); } IngredientCategory.SaveDatabase(typeof(IngredientCategory).Name); Ingredient.SaveDatabase(typeof(Ingredient).Name); Dish.SaveDatabase(typeof(Dish).Name); this.lstCategory.Items.Remove(selectedItem); MessageBox.Show("已删除此类别!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private bool TryAdd() { string name = this.txtName.Text.Trim(); if (string.IsNullOrEmpty(name)) { MessageBox.Show("食材类别的名称不能为空!"); return(false); } this.category.Name = this.txtName.Text.Trim(); IngredientCategory.SaveDatabase(typeof(IngredientCategory).Name); return(true); }