Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to bind Windows Forms properties to ApplicationSettings in C#?

Tags:

c#

.net

In a desktop application needing some serious re-factoring, I have several chunks of code that look like this:

private void LoadSettings()
{
    WindowState = Properties.Settings.Default.WindowState;
    Location = Properties.Settings.Default.WindowLocation;
    ...
}

private void SaveSettings()
{
    Properties.Settings.Default.WindowState = WindowState;
    Properties.Settings.Default.WindowLocation = Location;
    ...
}

What's the best way to replace this? Project-imposed constraints:

  • Visual Studio 2005
  • C# / .NET 2.0
  • Windows Forms

Update

For posterity, I've also found two useful tutorials: "Windows Forms User Settings in C#" and "Exploring Secrets of Persistent Application Settings".

I've asked a follow-up question about using this technique to bind a form's Size here. I separated them out to help people who search for similar issues. ­­­­­­­­­­­­­­­­­­­

like image 938
Brian Jorgensen Avatar asked Aug 20 '08 17:08

Brian Jorgensen


People also ask

How can you save the desired properties of Windows Forms application?

A simple way is to use a configuration data object, save it as an XML file with the name of the application in the local Folder and on startup read it back.

How does Windows form store value?

Solution 1. Create a Static class that holds the User name and password and any other variables need in entire application. Now you can access the UserID from anywhere in your code, or set a value from any where.


1 Answers

If you open your windows form in the designer, look in the properties box. The first item should be "(ApplicationSetting)". Under that is "(PropertyBinding)". That's where you'll find the option to do exactly what you want.

like image 192
Tundey Avatar answered Sep 21 '22 05:09

Tundey