Hi, I want to know how to save elements properties even after the form closes, and resume it back when I open the form.

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

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

        private void apply_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                MessageBox.Show("Checkbox1 is checked");
            }
            else if (checkBox2.Checked == true)
            {
                MessageBox.Show("Checkbox2 is checked");
            }
            else
            {
                MessageBox.Show("Checkbox3 is checked");
            }
        }

        private void load_Click(object sender, EventArgs e)
        {

        }

        private void save_Click(object sender, EventArgs e)
        {

        }






    }
}

Suppose I check the checkbox2 and click apply button, it should save and close.
When I open back the form then the checkbox2 should be checked.
And also if I have checked checkbox3 and click save button it should save the settings and by clicking on load button it should load back(i.e checkbox3 should be checked)

p.s how to save the setting data?

Edited 4 Years Ago by Andy90: n/a

You could store your settings in the application resources and read them back in.
You could do some Serialization.
One question : check your if statement. What if all 3 checkboxes are checked?
If that is not the behaviour you want, use radiobuttons instead.

To write to a text file use a System.IO.StreamWriter. Lots of (usually older) programs use an INI file that consists of sections and keys. It's also common to write to the registry. This looks like a good resource for both.

I prefer to use XML for its readability and scalability though.

As for actually getting this data to and from these different files, you will want to have a method for saving the current Form state. If it's just a matter of checkboxes...

(Example using StreamWriter/Reader)

//To save the settings
            StreamWriter myStream = new StreamWriter("MySettings.INI",false);
            myStream.WriteLine("[FormSettings]");
            foreach (CheckBox c in this.Controls.OfType<CheckBox>())
                myStream.WriteLine(string.Format("{0}={1}", c.Name, c.Checked.ToString()));
            myStream.Close();

//To load the settings
            StreamReader myIn = new StreamReader("MySettings.INI");
            string tData = "";
            string sControlName = "";
            bool bState = false;
            string[] sSplit;
            while (!myIn.EndOfStream)
            {
                tData = myIn.ReadLine();
                if (tData[0] == '[')
                    continue;
                try
                {
                    sSplit = tData.Split('=');
                    sControlName = sSplit[0];
                    bState = bool.Parse(sSplit[1]);
                    ((CheckBox)this.Controls[sControlName]).Checked = bState;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            myIn.Close();

Note that this is really primitive. There's quite a few premade libraries out there for dealing with settings files (INI and others). This should give you a good handle on reading and writing to text files with data from controls though.

Edited 4 Years Ago by skatamatic: n/a

Thanks skatamatic this works perfectly!
But what if my form contains checkbox, radio button, slider, combobox etc.
Its not loading!
how to do that?

Edited 4 Years Ago by Andy90: n/a

Thanks skatamatic this works perfectly!
But what if my form contains checkbox, radio button, slider, combobox etc.
how to do that?

You will have to get a bit more creative. Not every type of control has the same members (ie Textboxes have no checked).

There are more than one way to skin this cat, and using the actual control names to save this data is probably not the best way to do things but at the same time it's pretty dynamic. Here's an example that saves checkboxes AND textboxes. I'm not going to post anymore examples so I hope you see a pattern and can figuire out how to do other controls.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            StreamWriter myStream = new StreamWriter("MySettings.INI", false);
            myStream.WriteLine("[FormSettings]");
            foreach (Control c in this.Controls)
                if (c is CheckBox)
                    myStream.WriteLine(string.Format("{0}={1}", c.Name, ((CheckBox)c).Checked.ToString()));
                else if (c is TextBox)
                    myStream.WriteLine(string.Format("{0}={1}", c.Name, c.Text.ToString()));
            myStream.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader myIn = new StreamReader("MySettings.INI");
            string tData = "";
            string sControlName = "";
            string[] sSplit;
            Control tempControl;
            while (!myIn.EndOfStream)
            {
                tData = myIn.ReadLine();
                if (tData[0] == '[')
                    continue;
                try
                {
                    sSplit = tData.Split('=');
                    sControlName = sSplit[0];
                    tempControl = this.Controls[sControlName];
                    if (tempControl is CheckBox)
                        ((CheckBox)this.Controls[sControlName]).Checked = bool.Parse(sSplit[1]);
                    else if (tempControl is TextBox)
                        ((TextBox)this.Controls[sControlName]).Text = sSplit[1];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            myIn.Close();
        }
Comments
Great effort!

I m not able to load settings!
i.e checkbox should get ticked

Edited 4 Years Ago by Andy90: n/a

Why not? Are you sure that your Form is subscribing to these Load and Closing events? Post your code and the Designer.cs code please (unless it's really long).

Got it! I did some mistake! Thanks..
If I'll face any difficulty/problem I'll pm you!
Thanks once again skatamatic!
Solved!

This question has already been answered. Start a new discussion instead.