2

I hope I can explain and sorry for the text will be different language. What I am trying to do is I have two windows one is main and another is for csv file save. In my current program when I check checkbox and click Ok button it will work but checkbox will be unchecked after closing but I need to remain checked the checked box after open so I can easily find out what I checked last time. My windows is like:

enter image description here

Sorry my windows is in different language but I think it doesn't matter for this

niksan karkee
  • 157
  • 1
  • 17
  • Do you mean after the whole program is closed or you want to retrieve the value after the second window is closed into the first one ? – Ahmed Soliman May 25 '18 at 01:10
  • It appears you will need to pass the “checked” value back to the caller so the caller will know what the “last” checked state was. Then, when opening the save form the next time… you would pass the “last” checked state value back to the save form. – JohnG May 25 '18 at 01:13
  • @ahmed.soli thank you for your interest. No I don't mean the whole program is closed. i want to retrieve the value after the second winwo is closed – niksan karkee May 25 '18 at 01:14
  • @JohnG thank you for your comment how can I pass value back to the caller – niksan karkee May 25 '18 at 01:24
  • @niksankaree I have added an answer with working sample, try it : It will save the status of UI even on restart of the software. Let me know if you want anything additional in that – Amit May 25 '18 at 03:40

3 Answers3

3

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();
        }
    }
}
Amit
  • 1,821
  • 1
  • 17
  • 30
1

I think it's so simple , I assume you have first form named Form1 and second form named Form2 . The idea is to use this function that checks and returns the value checked and on reopening form2 the checkbox will be check or unchecked based on the stored value :

  • In form 2 make the checkbox Modifiers = public

    //Form 1
    public bool status;                         // create global string to be accessed in form2
    Form2 fm2 = new Form2();                    // make an instance of form 2
    fm2.stored_status = status;                 // assign the stored value to the getter and setter in form 2
    fm2.ShowDialog();                           // open form2 in dialog style            
    status = fm2.GetStatus(fm2.checkBox1);      //call the function and store
    
    // Form 2
    public bool stored_status { get; set; } // define getter and setter 
    
    private void Form2_Load(object sender, EventArgs e) 
    {
        if (stored_status)      // check if stored value equal checked
            checkBox1.Checked = true;           // make the checkbox checked
        else                                    // if not 
            checkBox1.Checked = false;          // keep it unchecked
    }
    
    public bool GetStatus(CheckBox check) // returns true of false and takes paramter of checkbox
    {
        if (check.Checked)           // check if checkbox1 is checked
             return true;               // return true
        else                                 // if not 
            return false;           // return false
    }
    
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16
  • When `Form2` loads… how is it going to know what the previous value was? `Form1` will need a global variable, which it can pass to the `Form2` constructor: `new Form2(PreviousValue)`. – JohnG May 25 '18 at 01:56
  • @JohnG I get you now . you want to get the value in the second form then when you open it again and for example if it was check is should be checked automatically right ? – Ahmed Soliman May 25 '18 at 02:06
  • Correct. Make `status` global and initialize it to false. Then pass it to `Form2(status)` constructor. Then form 2 can set the check box to the previous state on load. – JohnG May 25 '18 at 02:08
  • @ahmed.soli sorry soil it was not worked it only give a information if value is checked or not. What i need is if i checked something and click ok and after sometime again open form2 it should have to show last checked value – niksan karkee May 25 '18 at 02:18
  • @niksankaree My first version was based on what i undersood from you , now I'm working on what you need please wait – Ahmed Soliman May 25 '18 at 02:21
  • @ahmed.soli thank you so much for your contribute I will be waiting your answer – niksan karkee May 25 '18 at 02:29
1

This is not that difficult. Form1 needs one (1) Boolean global variable currentCheckState to hold the previous state of the Form2 check box. When created initialize it to false.

Form2 needs a little adjustment. First, a “constructor” that takes a Boolean variable is needed to set the check box to the previous state. In this approach, the check box can be set in the constructor itself. Second, we need a public ‘Boolean’ variable to hold the state of the check box when the user clicks the save button and closes the form.

Once Form2 closes, Form1 can set its global currentCheckState to the new check box state and the process repeats. Below is an example of what is described above.

Form2

public partial class Form2 : Form {

  public bool currentState;

  public Form2(bool previousState) {
    InitializeComponent();
    checkBox1.Checked = previousState;
  }

  private void btnSave_Click(object sender, EventArgs e) {
    currentState = checkBox1.Checked;
    this.Close();
  }
}

Form1

public partial class Form1 : Form {

  private bool currentCheckState = false;

  public Form1() {
    InitializeComponent();
  }

   private void button1_Click(object sender, EventArgs e) {
    Form2 f2 = new Form2(currentCheckState);
    f2.ShowDialog();
    currentCheckState = f2.currentState;
  }
}

Hope this makes sense.

JohnG
  • 9,259
  • 2
  • 20
  • 29
  • If there are multiple controls (like in question there are three checkboxes), it will be very awful to pass status of each control separately in Form's Constructor. – Amit May 25 '18 at 03:44
  • Pass a `List` or any collection type... build one if needed. – JohnG May 25 '18 at 03:45
  • Of course in this Post that will fit. but why not a "Status Holder Class" which have properties for every control and use that object for loading UI? won't that be more clear to understand and readable? – Amit May 25 '18 at 03:47
  • @Amit Absolutely, if you are trying to grab a bunch of data and pass it back to form1, you may want to rethink the whole approach, but if necessary as you stated a “Class” would do nicely if there are multiple items to pass back. – JohnG May 25 '18 at 03:49
  • Thanks to developers, Objects are reference type and we don't have to worry about passing status back to calling form! :) modifying object in child form will surely change the object of parent form (as they both have same reference) :D – Amit May 25 '18 at 03:53