Saving the state of the form, then opening it in the same state

I have a small program in winforms that contains 3 buttons. While the program allows the user to change the color of another button by pressing the corresponding button. While the third button does nothing. I want the user to save the changes made to the form (save the state of the form). Therefore, when the form opens again, it opens in the same state as it was saved.

I hope I will be clear that I am after

Here is the visualization of the form:

enter image description here

The code 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; } } } 
+4
source share
3 answers

This may not be easy 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 using this object:

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

In the form loading mode, check if the config exists, and 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 closes .. save the configuration:

 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 participants to your state class, and they will be written to the config.xml file.

+7
source

If you are viewing project property pages, you can add a settings file.

To use the settings in the code, you should do something like:

 Properties.Settings.Default.SettingName 

Do not forget that these settings are local and must be specified on each machine.

Code example:

  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

+6
source

usually when it comes to save the user interface settings, the standard way is to use an XML file to save or load the parameter that I made in this example to save the user interface components using xml, hope this will be useful

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

0
source

All Articles