I have made a Win Form and have a few controls like checkboxes, radio buttons etc. What I hope to happen is that the user will choose some settings e.g. tick the box to start the program on startup, then they can quit, when they open it again, how do I ensure that the choices that they made are saved? Thanks.
3 Answers
There are a few ways, but I'd recommend using the .NET user settings method to save their settings in the properties section of the application and reload and set them when they start the application again.
Here's an example:
Save Setting
Properties.Settings.Default.CheckboxChecked = true;
Properties.Settings.Default.Save();
Load Setting
checkBox.Checked = Properties.Settings.Default.CheckboxChecked;
I'd recommend giving them more meaningful names, however.
You can read more, with examples here: MSDN Using Application Settings and User Settings
This is also a nice tutorial on how to implement user settings from start to finish: C# - Saving User Settings - Easy way!

- 9,850
- 5
- 34
- 41
-
Thanks for the info, looks good but when I get as far as Properties.Settings.Default. I don't really have many options, how do I get the CheckboxChecked bool value after the Default? Thanks. – Bali C Sep 12 '11 at 16:15
-
If you have a look at the tutorial (the second link) it explains how to add the properties themselves via visual studio. You'll then be able to reference them like the example – Iain Ward Sep 12 '11 at 16:24
http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx Here is and Article how to use Settings in C# Application .
Where you can check ,if CheckBox is Checked with a Boolean etc.

- 10,761
- 11
- 59
- 89
Perhaps you’re looking for something like this:
Add this:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
Then this to the program:
for_save info = new for_save();
string general_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = general_path + "\\MyApplication";
BinaryFormatter serializer = new BinaryFormatter();
info.check = true;
info.radio = false;
//write
Directory.CreateDirectory(path);
Stream write_stream = File.Create(path + "\\MyFile.txt");
serializer.Serialize(write_stream, info);
write_stream.Close();
//read
Stream read_stream = File.OpenRead(path + "\\MyFile.txt");
for_save read_info = (for_save) serializer.Deserialize(read_stream);
read_stream.Close();
textBox1.Text = read_info.check.ToString() + read_info.radio.ToString();
And this class:
[Serializable()]
class for_save
{
public bool check;
public bool radio;
}

- 26,556
- 38
- 136
- 291