Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed a Winform in Visual Studio which has multiple tabs.

Some of the tabs have a textbox and some have both a textbox and a picturebox, with a button to upload images into the picturebox and the ability to enter text in the textbox.

What I would like to do is to be able to click "Save as.." and save it as some filename which then saves the data (text and images) and populates the tabs in the Winform with the saved data when I re-open the file at a later date.

I should state that I'm not a coder which is why I am here seeking help. The code below took a couple of days to write due to many hours spent reading through forums.

Any help on how to go about saving the data will be much appreciated.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// This is the code for your desktop app.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.

namespace TradingPlanFinal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        

        private void ButtonTechSetup_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string fileName = openFileDialog1.FileName;
            this.pictureBoxTechSetup.Image = System.Drawing.Image.FromFile(fileName);
        }

        private void ButtonTradeManagement_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string fileName = openFileDialog1.FileName;
            this.pictureBoxTradeManagement.Image = System.Drawing.Image.FromFile(fileName);
        }

        private void ButtonBigPic_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            string fileName = openFileDialog1.FileName;
            this.pictureBoxBigPic.Image = System.Drawing.Image.FromFile(fileName);
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }
    }
}
Posted
Updated 2-Apr-20 8:22am

1 solution

You can define a class holding all of this data and declare this class as serializable.
C#
[Serializable]
public class FormData
{
   public string TextBox1Data { get; set; }
   // ...
   public Image PictureBox1Data { get; set; }
   // ...
}

// Then, in form class
private void SaveButton_Click(object sender, EventArgs e)
{
   using (SaveFileDialog dialog = new SaveFileDialog())
   {
      if (dialog.ShowDialog(this) == DialogResult.OK)
      {
         using (FileStream fs = new FileStream(dialog.FileName, FileMode.OpenOrCreate))
         {
            FormData data = new FormData();
            data.TextBox1Text = textBox1.Text;
            // ...
            data.PictureBox1Data = pictureBoxTechSetup.Image;
            // ...
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fs, data);
         }
      }
   }
}

private void LoadButton_Click(object sender, EventArgs e)
{
   using (OpenFileDialog dialog = new OpenFileDialog())
   {
      if (dialog.ShowDialog(this) == DialogResult.OK)
      {
         using (FileStream fs = new FileStream(dialog.FileName, FileMode.Open))
         {
            BinaryFormatter formatter = new BinaryFormatter();
            FormData data = (FormData)formatter.Deserialize(fs);
            textBox1.Text = data.TextBox1Text;
            // ...
            pictureBoxTechSetup.Image = data.PictureBox1Data;
            // ...
      }
   }
}
 
Share this answer
 
v2
Comments
Member 14790578 11-Apr-20 8:22am    
Thank you Phil, this seems like it's getting close.
I've started a new project just to test this code and understand the concept and I'm hitting a minor glitch, which seems to be the only error that VS is throwing out.

On Line

FormData data = (data)formatter.Deserialize(fs);

I get red squiggles under "(data)" with error message
'data' is a variable but is used like a type

and the build won't run as a result.

Is there anything you could recommend I do to fix this?

Thank you
Brad
phil.o 11-Apr-20 10:18am    
Sorry, that's a mistake from me. This should read
FormData data = (FormData)formatter.Deserialize(fs);
Member 14790578 11-Apr-20 8:46am    
I've searched MS Docs online for error code CS0118 but it doesn't offer much insight in terms of a solution, other than saying the namespace and class can't be the same, which they aren't in this case.
Member 14790578 11-Apr-20 13:57pm    
So I changed (data) to (FormData) and it worked.

You are a gentleman Phil, thank you for sharing your knowledge. I've been working on this for weeks and today it all came together.
phil.o 12-Apr-20 3:56am    
You're welcome! Glad you sorted it all out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900