Bruno Klein
Bruno Klein

Reputation: 3367

What is the appropriate way to load WinForms settings to GUI

I am building an application, which has a form where the user can configure all his settings. When the application is loaded, the previously configured settings should reflect to the GUI (The UI should be consistent to the saved settings).

What I am currently doing is creating the settings on the project properties and I have a LoadSettings() method, which gets the values and outputs them to each component on the UI.

The thing is that this is getting VERY messy, and I don't like it at all.

So, that got me wondering, what are the correct approaches to achieve what I want, but yet getting high quality code? Any patterns for that?

private void LoadConfigs()
        {
            checkBoxStartOnStartup.Checked = ExistKeyValue(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "Wallbase Downloader");
            checkBoxCheckWallbaseOnline.Checked = Settings.Default.CheckWallbaseOnlineStartup;

            comboBoxResolution.SelectedIndex = comboBoxResolution.FindStringExact(Settings.Default.Resolution == string.Empty 
                ? GetScreenResolution() 
                : Settings.Default.Resolution);

            comboBoxCondition.SelectedIndex = Settings.Default.ConditionIndex;

            textBoxWallpaperFolders.Text = Settings.Default.WallpaperFolder;

            numericChangeInterval.Text = Convert.ToString(Settings.Default.ChangeIntervalValue);
            comboBoxChangeInterval.SelectedIndex = Settings.Default.ChangeIntervalIndex;

            numericCheckInterval.Text = Convert.ToString(Settings.Default.CheckIntervalValue);
            comboBoxCheckInterval.SelectedIndex = Settings.Default.CheckIntervalIndex;

            numericWallpapersToLookFor.Text = Settings.Default.WallpapersToLookFor.ToString();
        }

Upvotes: 0

Views: 157

Answers (2)

Jake
Jake

Reputation: 11430

You can use a Hashtable and use English strings for key to make your code really readable. Then serialize it to file on exit and deserialize it back when application loads. Save the serialized file to some common location so that you do not lose it.

Upvotes: 0

Szymon
Szymon

Reputation: 43023

Well, WinForms are not the cleanest framework around...

What you could do is to load all settings when your application starts up and store them in some storage that is available to all forms, e.g. in a static property in a helper settings class.

You can then access that static property from each form when it loads and make all necessary changes to the form based on the settings.

Upvotes: 2

Related Questions