Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Overriding Application Settings (now with example app)
 

Overriding Application Settings (now with example app)

Hello,

I've developed a Windows Forms application with several controls divided over a number of tab pages.
To allow the user to store their settings, I bound properties of the controls to entries in the Settings.settings.
This works all fine.
However, the application loads these settings by default. I want to somehow override this behavior.
If I start my application I want to choose if I load my settings from the settings file or from some other data source (shared process memory).

It would be nice if could simply skip loading the application settings file and set all values from memory. However I did not find an easy way to skip loading and still be able to save the settings whenever the user chooses to (otherwise simply not binding the application settings would be an option). So I tried a workaround.

Currently I simply allow the Settings.settings to set all controls initially and I override the values later on in the Form_Load event. This solution only partly works. Not all controls have their binding with the Settings.settings before form_load. Controls on tabPages that are not focused at startup get their values set when the tabPage gets focus, which overrides my custom settings that have been set in the form_load event.

For example, I create the following control (generated by Visual Studio):

//
// chkAutoExposure0
//
this.chkAutoExposure0.Checked = global::Tuner.Properties.Settings.Default.autoExposure0;
this.chkAutoExposure0.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Tuner.Properties.Settings.Default, "autoExposure0", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

this.chkAutoExposure0.Name = "chkAutoExposure0";
this.chkAutoExposure0.Text = "Auto Exposure";
this.chkAutoExposure0.CheckedChanged += new System.EventHandler(this.chkAutoExposure_CheckedChanged);



Now when the form is initialized the databinding with the application settings takes place and the intial values are set. Simply overriding the settings after the call to InitializeComponent does not work as the databinding later on updates the values again. Performing the override in the form_load event as shown below works for some controls.


private void MainForm_Load(object sender, EventArgs e)
{
if (_skipApplicationSettings)
{
chkAutoExposure0.Checked = sharedMemory.autoExposure;
}
}


However if chkAutoExposure is located on another tab, it still fails. Somehow the databinding does not accept the changes set on form_load and overrides them when the control is shown with its own value.

Summary: I'm looking for a solution that allows me to
- skip loading the application Settings.settings and set custom values to the controls properties that are bound to the settings file
- find some way that allows me to override the values that are set by the Settings.settings at startup.



  • Edited byEdsger Friday, September 18, 2009 8:50 PM
  •  
Edsger  Thursday, September 17, 2009 10:03 AM

Hi Edsger,

In your code snippet, the Checked property of the CheckBox is set to true, but the CheckBox is unselected when the form is shows. This is because the data bound to the Checked property is false. The value from the setting(false) would overwrite the value you set in the setting load event handler(true).

When we bind a data source to the controls, we need to know that the binding source now becomes the underlying data source. So that if we want to modify the data of the control programmatically, we need to modify the data source, not the control object directly. The code snippet below show my idea:

    //true: load data from setting; false: load data from shared memory.
    private bool _isLoadSetting = false;
    void Default_SettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e)
    {
        if (_isLoadSetting)
        {
            //Get data from shared memory.
            Settings.Default.check1 = sharedMemory.autoExposure;
            //Set other properties here.
        }
        else
        {
            //Get data from setting.
            Settings.Default.Reload();
        }
    }


Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byEdsger Tuesday, September 22, 2009 11:48 AM
  •  
Aland Li  Tuesday, September 22, 2009 7:54 AM
I found out that by using the SettingsLoadedevent, the settings read from the configuration file can be overwritten. Or at least that should happen. But in an example application to which I've provided a link below, it clearly shows that this somehow doesn't work. I really do not understand why it fails to work properly.

In the following example application I store the checked value of a checkbox in a settings file. But I overwrite the setting with the value true. If you start the application you see it doesnt work. Why?

example
Edsger  Friday, September 18, 2009 8:48 PM

Hi Edsger,

In your code snippet, the Checked property of the CheckBox is set to true, but the CheckBox is unselected when the form is shows. This is because the data bound to the Checked property is false. The value from the setting(false) would overwrite the value you set in the setting load event handler(true).

When we bind a data source to the controls, we need to know that the binding source now becomes the underlying data source. So that if we want to modify the data of the control programmatically, we need to modify the data source, not the control object directly. The code snippet below show my idea:

    //true: load data from setting; false: load data from shared memory.
    private bool _isLoadSetting = false;
    void Default_SettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e)
    {
        if (_isLoadSetting)
        {
            //Get data from shared memory.
            Settings.Default.check1 = sharedMemory.autoExposure;
            //Set other properties here.
        }
        else
        {
            //Get data from setting.
            Settings.Default.Reload();
        }
    }


Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byEdsger Tuesday, September 22, 2009 11:48 AM
  •  
Aland Li  Tuesday, September 22, 2009 7:54 AM
Hello Aland Li,

Thank you for your answer.
Indeed, I thought I could trigger the datasource by updating the control. Just as if a user changed the control from the GUI. As you've made clear, this does not work (at least at startup) because of the underlying model.

Thanks!


Edsger  Tuesday, September 22, 2009 11:48 AM

You can use google to search for other answers

Custom Search

More Threads

• .NET Runtime 2.0 Error Reporting
• Clear or hide specific cell in datagrid view
• Refresh DataSource of DataGridView Inside ToolStripDropDown
• Tab Control
• VS2008 Visual Basic: In defining a data relation between tables do the foreign key and primary key need to be identically named?
• assistance modifying HMI
• Datagridview Image Column
• Custom DataGridView editing control obliterated during column resize.
• listbox on ado.net
• Combobox in DataGridView Cell problem