示例#1
0
        public void serialize(MyRecipe Ar)
        {
            Ar.Name = txtName.Text;
            //Ar.PrepTime = Int32.Parse(txtPrepTime.Text);
            Ar.Ingredients = txtIngredients.Text;
            Ar.Methods     = lbSteps.Text;
            //Ar.Serving = Int32.Parse(txtNumServes.Text);
            Ar.Category = cbCategory.Text;

            //open a file and serialize objects into binary format
            Stream          stream = File.Open("favourite_recipes.osl", FileMode.Create);
            BinaryFormatter bf     = new BinaryFormatter();

            bf.Serialize(stream, Ar);
            stream.Close();
            MessageBox.Show("Recipe is added as favourite");
            chkbox_fav.Checked = false;

            //deserialize and load
            string Error;

            try
            {
                stream = File.Open("favourite_recipes.osl", FileMode.Open);
                bf     = new BinaryFormatter();
                Ar     = (MyRecipe)bf.Deserialize(stream);
                stream.Close();
                Console.WriteLine("name: {0}", Ar.Name);
                Console.WriteLine("prep time: {0}", Ar.PrepTime);
                Console.WriteLine("Ingredients: {0}", Ar.Ingredients);
                Console.WriteLine("Serving: {0}", Ar.Serving);
                Console.WriteLine("Methods: {0}", Ar.Methods);
                Console.WriteLine("Category: {0}", Ar.Category);
                //Add item to list view

                if (lvFavourite.Columns.Count < 1)
                {
                    lvFavourite.Columns.Add("Name", 100);
                    lvFavourite.Columns.Add("Ingredients", 100);
                    lvFavourite.Columns.Add("Steps", 100);
                    lvFavourite.Columns.Add("Category", 100);
                    lvFavourite.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { txtName.Text, txtIngredients.Text, lbSteps.Text, cbCategory.Text }));
                }
                else
                {
                    lvFavourite.Items.Add(new System.Windows.Forms.ListViewItem(new string[] { txtName.Text, txtIngredients.Text, lbSteps.Text, cbCategory.Text }));
                }
                lvFavourite.View = View.Details;
                Controls.Add(lvFavourite);
            }
            catch (Exception exception)
            {
                Error = exception.ToString();

                // TODO: catch other exceptions
                Console.WriteLine(exception);
            }
        }
示例#2
0
        public void deserializeAndLoad(MyRecipe Ar)
        {
            Stream FileStream;

            Ar = null;
            BinaryFormatter bf = new BinaryFormatter();
            string          Error;

            try
            {
                FileStream = File.Open("favourite_recipes.osl", FileMode.Open);
                bf         = new BinaryFormatter();
                Ar         = (MyRecipe)bf.Deserialize(FileStream);
                FileStream.Close();
                Console.WriteLine("name: {0}", Ar.Name);
                Console.WriteLine("prep time: {0}", Ar.PrepTime);
                Console.WriteLine("Ingredients: {0}", Ar.Ingredients);
                Console.WriteLine("Serving: {0}", Ar.Serving);
                Console.WriteLine("Methods: {0}", Ar.Methods);
                Console.WriteLine("Category: {0}", Ar.Category);
                //Add item to list view
                System.Windows.Forms.ListViewItem lvi = new System.Windows.Forms.ListViewItem();
                lvi.SubItems.Add(Ar.Name);
                lvi.SubItems.Add(Ar.Ingredients);
                lvi.SubItems.Add(Ar.Category);
                lvi.SubItems.Add(Ar.Methods);
                lvFavourite.Items.Add(lvi);
            }
            catch (Exception exception)
            {
                Error = exception.ToString();

                // TODO: catch other exceptions
                Console.WriteLine(exception);
            }
        }