Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Application Settings Cancel Button
 

Application Settings Cancel Button

I love databinding controls to application settings. I am trying to make an options dialog for my program, and I have an accept and cancel button on my form. I want the accept button to save all the properties to the settings file and the cancel button to restore them to their pre-dialog values. This should be simple enough. I've got bindings setup, and they save automatically (on TextChanged). Now how do I get the cancel button to do something useful?

MattSchultz  Monday, May 19, 2008 11:29 PM

This is because by default, whencontrols data bound to a source, theDataSourceUpdateMode is set to OnValidation, which means, the source will be update when the control property is validated, when you click on the save button, the focus leaves from the editing control, and if the input is validated, the changes will be automatically affect to the application settings.

To avoid this, we can set up the binding by code manually without using designer, set the DataSourceUpdateMode to Never, and save the changes to the application settings when clicks on the save button by calling the WriteValue() method of the Binding object.

See my sample for the details

Code Snippet

partial class Form14 : Form

{

public Form14()

{

InitializeComponent();

}

private void Form14_Load(object sender, EventArgs e)

{

this.textBox1.DataBindings.Add("Text",

Properties.Settings.Default, "test",true, DataSourceUpdateMode.Never);

}

private void button1_Click(object sender, EventArgs e)

{

//force the TextBox to write the changes back to the source

this.textBox1.DataBindings[0].WriteValue();

MessageBox.Show(Properties.Settings.Default.test);

}

private void button2_Click(object sender, EventArgs e)

{

MessageBox.Show(Properties.Settings.Default.test);

}

}



Best Regards
Zhi-xin Ye

Zhi-Xin Ye  Thursday, May 22, 2008 8:19 AM

This is because by default, whencontrols data bound to a source, theDataSourceUpdateMode is set to OnValidation, which means, the source will be update when the control property is validated, when you click on the save button, the focus leaves from the editing control, and if the input is validated, the changes will be automatically affect to the application settings.

To avoid this, we can set up the binding by code manually without using designer, set the DataSourceUpdateMode to Never, and save the changes to the application settings when clicks on the save button by calling the WriteValue() method of the Binding object.

See my sample for the details

Code Snippet

partial class Form14 : Form

{

public Form14()

{

InitializeComponent();

}

private void Form14_Load(object sender, EventArgs e)

{

this.textBox1.DataBindings.Add("Text",

Properties.Settings.Default, "test",true, DataSourceUpdateMode.Never);

}

private void button1_Click(object sender, EventArgs e)

{

//force the TextBox to write the changes back to the source

this.textBox1.DataBindings[0].WriteValue();

MessageBox.Show(Properties.Settings.Default.test);

}

private void button2_Click(object sender, EventArgs e)

{

MessageBox.Show(Properties.Settings.Default.test);

}

}



Best Regards
Zhi-xin Ye

Zhi-Xin Ye  Thursday, May 22, 2008 8:19 AM

Ah, ok, that makes sense, though it is less than ideal. I almost want to hack this out and simply write an itterator which does this automatically in the constructor, though obviously this is a big fat hack, but it would at least let me still use the designer, which does rock. Any chance to see this choice offunctionality in a future version?

MattSchultz  Sunday, June 01, 2008 12:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Sorting DataGridView requires manual mouse clicks?
• Datagrid problem
• Setting alternatingback color for grid with combobox column
• Checking for Match
• Datagridview column wrapping?
• DataGridView without active edit cell
• Datagridview combobox after update
• Accessing Bound DataTable Index from DataGridView
• Highlighting/setting the first item in my combobox datagridview
• The requested operation requires delegation to be enabled on the machine.