Home > Net >  (ApplicationSettings) not showing on form properties - Visual Studio 2022 C#
(ApplicationSettings) not showing on form properties - Visual Studio 2022 C#

Time:08-26

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):

enter image description here

The problem is that I looked everywhere and couldn't find that, mine appears like this:

enter image description here

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.

  1. Right click on you project and navigate to Properties > Settings > General. Click on "Create or open application settings".

  2. Add your setting, e.g., with Name = "DefaultBackColor" and Type = System.Drawing.Color.

  3. Close the project properties and compile.

  4. 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.

  5. In the Forms designer click on the background of your form to select it in the properties window.

  6. 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.

  7. 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.

  8. 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.

  • Related