Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Update Dependent Controls with Application Settings
 

Update Dependent Controls with Application Settings

In my application I use Application Settings to store user settings.
Some of these controls are linked. For example if a checkbox is checked, an edit control might get the text "ticked".
However, this dependency fails to update properly.

I've created a simplified version of my app in the example below. In the example I create two checkboxes, if the first one is checked, the second must be checked as well. This is the code:

//
// checkBox1
//
this.checkBox1.Checked = global::WindowsFormsApplication1.Properties.Settings.Default.check1;
this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::WindowsFormsApplication1.Properties.Settings.Default, "check1", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.checkBox1.Name = "checkBox1";
this.checkBox1.Text = "checkBox1";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);

//
// checkBox2
//
this.checkBox2.Checked = global::WindowsFormsApplication1.Properties.Settings.Default.check2;
this.checkBox2.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::WindowsFormsApplication1.Properties.Settings.Default, "check2", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.checkBox2.Name = "checkBox2";
this.checkBox2.Text = "checkBox2";
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);




private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.check2 = checkBox1.Checked;
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}


However, when running this code. Checking checkbox1 immediately unchecks it.
Edsger  Thursday, September 24, 2009 8:35 AM
Bump.

Anybody?
Edsger  Monday, September 28, 2009 10:56 AM

Hi,

Sorry. I test on my side. I am not able to reproduce. When I click Checkbox1, checkbox2 is also checked.

I think I may miss something.

The following is my test:

private void Form1_Load(object sender, EventArgs e)

{

// Copy your code

this.checkBox1.Checked = global::Test0930.Properties.Settings.Default.check1;

this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Test0930.Properties.Settings.Default, "check1", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

this.checkBox1.Name = "checkBox1";

this.checkBox1.Text = "checkBox1";

this.checkBox1.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

this.checkBox2.Checked = global::Test0930.Properties.Settings.Default.check2;

this.checkBox2.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::Test0930.Properties.Settings.Default, "check2", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

this.checkBox2.Name = "checkBox2";

this.checkBox2.Text = "checkBox2";

this.checkBox2.CheckedChanged += new EventHandler(checkBox2_CheckedChanged);

}

void checkBox1_CheckedChanged(object sender, EventArgs e)

{

Settings.Default.check2 = checkBox1.Checked;

}

private void button1_Click(object sender, EventArgs e)

{

Console.WriteLine(Settings.Default.check2.ToString());

}

void checkBox2_CheckedChanged(object sender, EventArgs e)

{

}

Code in settings.Designer.cs:

[global::System.Configuration.UserScopedSettingAttribute()]

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

[global::System.Configuration.DefaultSettingValueAttribute("False")]

public bool check1 {

get {

return ((bool)(this["check1"]));

}

set {

this["check1"] = value;

}

}

[global::System.Configuration.UserScopedSettingAttribute()]

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

[global::System.Configuration.DefaultSettingValueAttribute("False")]

public bool check2 {

get {

return ((bool)(this["check2"]));

}

set {

this["check2"] = value;

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 30, 2009 2:42 PM

We are changing the issue type to “General Discussion” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, October 04, 2009 7:28 AM
Thank you for your response, however even after double checking my own very simple application I can't see any difference - except for the fact that the problem does occur in my solutoin. So I've uploaded my solution to:
this location

This should reproduce the problem.
Edsger  Tuesday, October 06, 2009 12:11 PM

You can use google to search for other answers

Custom Search

More Threads

• Disabling DataSet/DataTable update notifications to bound components
• Progressive search using ComboBox
• Updating ConnectionString of a Dataset at Run-Time
• Problem with datagrid ... pls help
• UserPreferenceChangedEventHandler
• DataGridView Combo box Problem
• datagridview newrow()
• Problem with Sum(child(relationName).field)
• how to empty a DataGridView ???
• unhandled Exception thrown when user enters value in a new row in datagridView