10

It's possible to bind a property to an existing application setting using the designer, that way I don't have to write something like

textBox.Text = Settings.Default.Name;

when initializing my form.

Now it looks like the binding stops there, ie if I change the value of my setting, it won't update the value in my textbox and vice versa.

Is it normal or am I missing something?

2 Answers 2

21

I'll explain how I do it on windows forms with Visual Studio 2010. It is likely similar in newer versions.

  1. Click on the component to bind (textbox, checkbox, ...)
  2. Make component's properties panel visible
  3. Expand the node ApplicationSettings
  4. Click the ... button to the right of (PropertyBinding)

    Get application settings for component

  5. Choose which setting you want the control bound to.

There is a more detailed tutorial at Exploring Secrets of Persistent Application Settings

6
  • 1
    How do you save, I have found that if I update the textbox and close my app when I reopen it, the old value is still there? Commented Jan 27, 2016 at 9:10
  • 1
    I've tested that closing the app using the stop debug button the user settings aren't saved. You should close the app correctly ( red cross button ). Commented Jan 27, 2016 at 9:54
  • Is there any code I can manually call to persists the settings? For example on text Changed listener? or every 10 seconds? Commented Jan 27, 2016 at 13:16
  • Sadly the link you refer to now 404s. Commented Sep 19, 2021 at 20:43
  • 1
    @AndrewTruckle try again, it seems that it is working now Commented Sep 20, 2021 at 7:17
1

Well, it depends.

First, I'm not sure if you're talking WPF or Windows Forms, so I'll assume neither.

Second, you are not "binding" anything. You are taking the value of Name and setting the property Text equal to this value. You are setting a property. This does not come with any magic side-effects which inextricably links the Name property to the Text property.

Third, you can change settings, but until you save them, they are not written back to your app.config. In a Windows Forms app, you'd have to do something like this:

// event handler for the Form.Closed event.
// this.FormClosed += FormClosed;
void FormClosed(object sender, FormClosedEventArgs e)
{
    Settings.Default.Name = textBox.Text;
    Settings.Default.Save();
}

In WPF, you'd use the normal binding semantics (which means you avoid the hassle of setting all the property values when closing), but you still have to trap for the form closing so you can Save() the settings.

Binding:

<TextBox 
  xmlns:lol="clr-namespace:MyApplication.Settings"
  Text="{Binding Name, Source={x:Static lol:Default}}" />

The Save() call happens much as with the Forms example, but you don't have to do anything other than call Save().

1
  • There's a misunderstanding here. I am not writing "textBox.Text = Settings.Default.Name;" thanks to the binding (see link). I know that if I want the changes to be saved to the setting file, I have to specifically call "Settings.Default.Save();".
    – user276648
    Commented Aug 20, 2010 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.