private void Form1_Load(object sender, EventArgs e) { //Add save function to delegate save = new Save(SaveRecipies); //Try-Catch block for file access try { //Instantiate FileStream object to read serialized data from file FileStream inFile = new FileStream("Recipies.txt", FileMode.OpenOrCreate, FileAccess.Read); //Loop through all of the data in the file while (inFile.Position < inFile.Length) { //Deserialize the objects stored in the file RecipeWithDrink recipe = (RecipeWithDrink)bFormatter.Deserialize(inFile); //Store objected in list lstRecipies.Items.Add(recipe); } inFile.Close(); } catch (Exception) { MessageBox.Show("Unable to load Recipies.txt\nProgram will now close"); this.Close(); } }
private void BtnView_Click(object sender, EventArgs e) { if (lstRecipies.SelectedItem != null) { RecipeWithDrink recipe = (RecipeWithDrink)lstRecipies.SelectedItem; using (ViewRecipe frmViewRecipe = new ViewRecipe(recipe)) { frmViewRecipe.ShowDialog(); } } }
public ViewRecipe(RecipeWithDrink recipe) { InitializeComponent(); txtName.Text = recipe.Name; txtInstructions.Text = recipe.Instructions; txtDrink.Text = recipe.DrinkPair.ToString(); foreach (string ingredient in recipe.Ingredients) { lstIngredients.Items.Add(ingredient); } }
//Overloaded constructor to edit existing recipe //Puts object data into appropriate fields public AddRecipe(RecipeWithDrink recipe) { InitializeComponent(); this.Text = "Edit Recipe"; btnAddRecipe.Visible = false; btnEditRecipe.Visible = true; txtName.Text = recipe.Name; txtInstructions.Text = recipe.Instructions; cmbxDringPair.SelectedItem = recipe.DrinkPair.ToString(); foreach (string ingredient in recipe.Ingredients) { lstIngredients.Items.Add(ingredient); } }
private void BtnEdit_Click(object sender, EventArgs e) { int index = lstRecipies.SelectedIndex; RecipeWithDrink recipe = (RecipeWithDrink)lstRecipies.SelectedItem; using (AddRecipe frmEditRecipe = new AddRecipe(recipe)) { frmEditRecipe.ShowDialog(); if (frmEditRecipe.DialogResult == DialogResult.OK) { lstRecipies.Items.Add(frmEditRecipe.recipe); lstRecipies.Items.RemoveAt(index); } } }
private void BtnEditRecipe_Click(object sender, EventArgs e) { Drink drinkPair; string name = txtName.Text; string instructions = txtInstructions.Text; List <string> ingredients = new List <string>(); Enum.TryParse(cmbxDringPair.SelectedItem.ToString(), out drinkPair); foreach (var item in lstIngredients.Items) { ingredients.Add(item.ToString()); } //Instatiate object with user input recipe = new RecipeWithDrink(name, instructions, drinkPair, ingredients); }