5

I have a small program in winforms that holds 3 buttons. Thus far the program lets the user change a the color of another button by clicking the corresponding button While the third button does not do anything yet. What I want to do is to let the user save changes made to the form (save the form state). So when the form is reopened it opens in that same state as saved.

I hope I am being clear about what I am after

Here is a visualization of the form:

enter image description here

The code that i have so far if any help:

public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnToColor.Text = "";
        }

        int c = 0;
        private void btnColorSwap_Click(object sender, EventArgs e)
        {
            if (c == 0)
            {
                btnToColor.BackColor = Color.Yellow;
                c++;

            }

            else if (c == 1)
            {
                btnToColor.BackColor = Color.YellowGreen;

                c++;
            }

            else if (c == 2)
            {
                btnToColor.BackColor = Color.LimeGreen;

                c = 0;
            }

        }
    }
Tacit
  • 890
  • 6
  • 17
  • 42
  • Do you want to save state just in that process, or between app executions? – squillman Jan 31 '13 at 01:27
  • I'm not sure what any of this means to be honest I'm looking for a simple way to save the state of the form – Tacit Jan 31 '13 at 01:31
  • I mean, do you want to save the state so that next time you run the program the form goes back to that state, or so that next time you open the form after just closing the form (without ending the program)? – squillman Jan 31 '13 at 01:36
  • I want to save the state so that next time I run the program the form goes back to that state – Tacit Jan 31 '13 at 01:39
  • check http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application – spajce Jan 31 '13 at 04:39

3 Answers3

7

This may/may not be easier for you.

Start by creating a class to hold your state:

public class MyFormState {
    public string ButtonBackColor { get; set; }
}

Now, declare a member for your Form with this object:

public partial class Form1 : Form {
    MyFormState state = new MyFormState();

On form load, check if the config exists, then load it:

private void Form1_Load(object sender, EventArgs e) {
    if (File.Exists("config.xml")) {
        loadConfig();
    }

    button1.BackColor = System.Drawing.ColorTranslator.FromHtml(state.ButtonBackColor);
}

private void loadConfig() {
    XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
    using (FileStream fs = File.OpenRead("config.xml")) {
        state = (MyFormState)ser.Deserialize(fs);
    }
}

When your form is closing.. save the config:

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    writeConfig();
}

private void writeConfig() {
    using (StreamWriter sw = new StreamWriter("config.xml")) {
        state.ButtonBackColor = System.Drawing.ColorTranslator.ToHtml(button1.BackColor);
        XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
        ser.Serialize(sw, state);
    }
}

Then you can add members to your state class and they will be written into the config.xml file.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • The solution works good. The problem is that in case you would need to do this for all items in a listbox, things get icky really fast if you iterate with a foreach loop through the items in the listbox and serialize each item(which you HAVE to do). On load you get a nice exception saying that you've already declared the XML namespace one at the beginning of the file and you're doing it each time you serialize. Any workaround that ? – lorddarq Jan 29 '14 at 09:03
  • Simply store them in a list of strings. The serializer will take care of it. – Simon Whitehead Jan 29 '14 at 09:23
6

If you have a look in the project property pages you can add a settings file.

To use the settings in code you would do something like:

Properties.Settings.Default.SettingName

Do bare in mind though that these settings are local and would need to be specified on each machine

Sample Code :

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.btn1 = button1.UseVisualStyleBackColor ? Color.Transparent : button1.BackColor;
        Properties.Settings.Default.btn2 = button1.UseVisualStyleBackColor ? Color.Transparent : button2.BackColor;
        Properties.Settings.Default.btn3 = button1.UseVisualStyleBackColor ? Color.Transparent : button3.BackColor;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        if (Properties.Settings.Default.btn1 != Color.Transparent) button1.BackColor = Properties.Settings.Default.btn1;
        if (Properties.Settings.Default.btn2 != Color.Transparent) button1.BackColor = Properties.Settings.Default.btn2;
        if (Properties.Settings.Default.btn3 != Color.Transparent) button1.BackColor = Properties.Settings.Default.btn3;
    }

Here is a link to the settings class on MSDN http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx


PropertyPages

Property Setting Page

Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
0

usually when it come to store an user interface setting the standard way is to use XML file to save or load the setting i made this example that save user interface components using xml hope it be useful

https://www.dropbox.com/s/1j1qbe7udqxizr6/4.XMLConfigurationEditor.rar?dl=0