Reputation: 53
In VS 2022 I create a new winform (.net 6.0) project. I put one textbox on the form. In the properties window, at the top, I'm used to seeing an item "ApplicationSettings" where I can bind the TEXT property of the textbox to an application setting. But I no longer see the line for "ApplicationSettings". If I open an older winform project it works as expected.
Upvotes: 4
Views: 7109
Reputation: 125197
The feature is (still) not available for WinForms .NET 6 but if you create a WinForms .NET framework project, it's available.
Data binding to application settings in code
You can setup the data binding in code:
Create or open the Application settings. (Project properties -> Settings -> Create or open application settings.)
Add a user-scoped property like Property1
(if you want it to be modifiable by user in a data-binding scenario.)
Setup a databinding in code, like this:
this.textBox1.DataBindings.Add(new Binding("Text",
Properties.Settings.Default, "Property1", true,
DataSourceUpdateMode.OnPropertyChanged));
Designer workaround
If you really want a designer-based solution, then:
Create or open the Application settings. (Project properties -> Settings -> Create or open application settings.)
Change the application settings class modifier to public
(on the top bar) to make it visible to the Add new data source window.
After building the project, in the databinding section of the property grid, for your control, add a new project data source and setup databinding to Settings. (Preferably advanced setting and choosing OnPropertyChanged.)
Then you need to set the data source when you load form:
settingsBindingSource.DataSource = Properties.Settings.Default;
Upvotes: 5