Easily save and restore all of a form's settings and the values of its controls in the Registry in C#
The example Save and restore settings in C# saves and restores a form's size and position in the program's settings.
This example saves and restores the form's size, position, and window state (normal, minimized, or maximized). It also saves the values in all of the form's controls. (This example handles TextBoxes, combo boxes, radio buttons, numeric up/down buttons, and scroll bars. You can use similar techniques to make it handle other controls if you like.)
The code is pretty long so I'm only going to describe it here. Download the example to see how it works.
Saving Values
The program saves the values in the system Registry. The SaveSetting function uses the Microsoft.Win32.RegistryKey class to make saving settings in the Registry easy. It takes a name and value as parameters and saves the value in the registry under that name.
The SaveAllSettings function uses SaveSetting to save the form's basic information. It first saves the form's WindowState. Then if the form is in its normal state, the function saves its current size and position. If the form is minimized or maximized, the function saves the form's restored bounds--the size and position it has when in the normal state. The function finishes by calling SaveChildSettings to save the values in the form's controls.
Function SaveChildSettings saves TextBox Text values, ComboBox Checked values, and so forth. It takes as a parameter a parent control and loops through the child controls that the parent contains. The function uses a switch statement to see what kind of control each child is and uses SaveSetting to save the appropriate value. It then recursively calls itself for the child to save the values in the child's children.
Loading Values
The GetSetting function uses the Microsoft.Win32.RegistryKey class to make restoring settings from the Registry easy. It takes a name and default value as parameters. It looks up the name and returns the saved value if it's in the Registry or the default value if it is not.
The LoadAllSettings function simply retrieves the saved form size and position and uses them to arrange the form. It also sets the form's WindowState. It finishes by calling LoadChildSettings to load the controls' saved values.
Function LoadChildSettings loops through a parent control's children. It uses GetSetting to restore the child's saved value and then recursively calls itself to load the values in the control's children.
Using the Functions
Having written these functions, using them is easy. I put them in the RegistryTools class to make them easy to move into new projects. The program simply calls the static LoadAllSettings and SaveAllSettings functions in its Load and FormClosing event handlers. The following code uses the application's product name to identify the section in the Regsitry where the values should be stored.
// Load the saved settings.
private void Form1_Load(object sender, EventArgs e)
{
RegistryTools.LoadAllSettings(Application.ProductName, this);
}
// Save settings.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
RegistryTools.SaveAllSettings(Application.ProductName, this);
}



Hello sir,
this is so informative i`ve been searching for something like that for a long time i`m new with registry ... and was coding a sticky note for my brother with a reminder and that is done but i want to make it when the reminder is set to another date (like tomorrow) and he shut down his computer it saves the date, time and note which is done by this tutorial ... and when he opens the computer it show up the note at the date and time .. hope you understood what i want :)
thanks again ..
Reply to this
You should be able to do that. Save each message with a date on which that message should be displayed. Then when the program starts, it can load the current day's message.
That approach would work but I might display all of the messages instead of just showing today's message. That way you can get a hint of what is coming over the next few days. (I may put together an example in the next few days.)
In fact, you could build a whole calendar application. You might want to look at some of the calendar programs that are out there to see what features they provide. (Or you could just buy one of them or get a free one ;-)
Reply to this
yeah well i was trying to make something like a calender but without a calender itself ... thanks anyways i`ll try that :)
Reply to this
It sort of depends on how many features you want. You can definitely do the post-it style popup on the day a message is due. Or you could build a full-blown calendar that would have a lot more features but be a lot more work.
Maybe you should start with the first idea and see how it goes. Just store both the date and message so the program can check the date when it starts.
Reply to this