I was looking for a way to create a sort of "global" setting for the background color of some forms in a project. Some examples I found suggested using this (ApplicationSettings) option on Properties, like this (sorry I'm new here so I can't post the image directly):
The problem is that I looked everywhere and couldn't find that, mine appears like this:
Is there a way to add that option?
CodePudding user response:
That option is available for projects targeting .NET Framework but not for those targeting .NET Core, which includes .NET 5 and later. Windows Forms was not originally supposed to make the trip to .NET Core but Microsoft realised that many people would sooner ignore .NET Core than abandon WinForms. You have a choice to make whether you want that functionality or you want to target .NET Core.
Note that you can probably just write the code yourself to bind a property to an application setting. I just created a binding for the Text
property of a form and the following code was generated in the designer code file:
this.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::MyWindowsFormsApp.Properties.Settings.Default, "Form1Text", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
You can basically write that code yourself in the Load
event handler, where you would likely bind other properties, e.g. to data retrieved from a database.
CodePudding user response:
The ability to bind directly to application settings in Windows Forms has not been migrated from the .NET Framework to .NET Core.
However, with some code and a workaround you can still bind to your settings.
Right click on you project and navigate to Properties > Settings > General. Click on "Create or open application settings".
Add your setting, e.g., with Name = "DefaultBackColor" and Type = System.Drawing.Color.
Close the project properties and compile.
Visual Studio has created a file Properties > Settings.settings > Settings.Designer.cs. Open Settings.Designer.cs. You will see an automatically generated
internal sealed partial class Settings
having a propertypublic Color DefaultBackColor { get; }
. To be able to create a binding source we must make this classpublic
and compile again. This class will be re-created later with theinternal
modifier, but we do not mind, as we need thepublic
modifier only temporarily.In the Forms designer click on the background of your form to select it in the properties window.
In the properties window select DataBindings > Advanced. In the Bindings combo box select "Add new Object Data Source...", select the
Settings
class and click OK.In my case Visual Studio did not update the Binding drop-down immediately. So, I had to fiddle around a bit, but finally I got the entry Other Data Sources > Project Data Sources > Properties > Settings > DefaultBackColor. In the left list select
BackColor
and then select thisDefaultBackColor
in the Binding drop-down. This creates asettingsBindingSource
on your form and wires up theBackColor
of your form.Set the data source of this binding source in the constructor of your form:
public Form1() { InitializeComponent(); settingsBindingSource.DataSource = Properties.Settings.Default; }
Now, the form and all the controls, which inherit the BackColor
from the form, will have the color defined in the application settings.
In your other forms repeat the steps 7 and 8.