I have uploaded working sample here
Explanation :
You can create a status class which holds status of all controls (checkboxes etc), which values they were holding lastly.
public class StatusClass
{
public bool Chkbox1Checked;
public bool Chkbox2Checked;
//same way other required controls status
// EG. public string Textbox1Tex
}
Now pass this classes object to your save csv form's constructor.
like below.
private StatusClass m_statusClass;
public FrmSaveCSV(StatusClass statusClass)
{
InitializeComponent();
m_statusClass = statusClass;
}
and you have to listen Load and Closing event of this form (FrmSaveCSV) where you can Load previous status and Save current status respectively. like below:
private void FrmSaveCSV_Load(object sender, EventArgs e)
{
LoadStaus();
}
private void FrmSaveCSV_FormClosing(object sender, FormClosingEventArgs e)
{
SaveStatus();
}
private void LoadStaus()
{
this.Checkbox1.Checked = m_statusClass.Chkbox1Checked;
this.Checkbox2.Checked = m_statusClass.Chkbox2Checked;
//same way load other controls
}
private void SaveStatus()
{
m_statusClass.Chkbox1Checked = this.Checkbox1.Checked;
m_statusClass.Chkbox2Checked = this.Checkbox2.Checked;
}
Now,
while closing entire application, you can simply serialize
this object of StatusClass and on startup of the software you de-serialize
it back and use it to pass it to the constructor of FrmSaveCSV
. this way you status saving will work even if you restart the entire software.
refer this question :
How to save/restore serializable object to/from file?
Note: Make sure, you are not creating new object of status class and pass this newly created object to FrmSaveCSV
every time. Object of status class must be same all the time.
meaning if you are calling this FrmSaveCSV
from Form1
it should look like this,
public partial class Form1 : Form
{
//common (single) object of status calss
StatusClass m_statusClass;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//passing common (single) object of status class in this form
FrmSaveCSV frmSaveCSV = new FrmSaveCSV(m_statusClass);
frmSaveCSV.Show();
}
//On software load, we de-serialize last status form file
private void Form1_Load(object sender, EventArgs e)
{
string fileName = "Status.dat";
if (File.Exists(fileName))
{
Stream s = new FileStream(fileName, FileMode.Open, FileAccess.Read);
IFormatter f1 = new BinaryFormatter();
try
{
m_statusClass = (StatusClass)f1.Deserialize(s);
}
catch
{
s.Close();
m_statusClass = new StatusClass();
}
s.Close();
}
else
m_statusClass = new StatusClass();
}
//Of software closing we serialize current stats in file.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string fileName = "Status.dat";
if (File.Exists(fileName))
File.Delete(fileName);
using (Stream s = new FileStream("Status.dat", FileMode.Create))
{
IFormatter f2 = new BinaryFormatter();
f2.Serialize(s, m_statusClass);
s.Close();
s.Dispose();
}
}
}