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?
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 property public Color DefaultBackColor { get; }
. To be able to create a binding source we must make this class public
and compile again. This class will be re-created later with the internal
modifier, but we do not mind, as we need the public
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 this DefaultBackColor
in the Binding drop-down. This creates a settingsBindingSource
on your form and wires up the BackColor
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.