Let us look at a simple C# Windows Forms Settings example.

Example 1: Settings

This example will comprise the following files:

  • Settings.Designer.cs
  • Settings.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). Settings.Designer.cs

Create a file named Settings.Designer.cs


// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
-----------------------------------------------------------------------------

```csharp

namespace Examples.Properties {

    
    
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        
        
        
        public global::System.Drawing.Color BackColor {
            get {
                return ((global::System.Drawing.Color)(this["BackColor"]));
            }
            set {
                this["BackColor"] = value;
            }
        }

        
        
        
        public int Height {
            get {
                return ((int)(this["Height"]));
            }
            set {
                this["Height"] = value;
            }
        }

        
        
        
        public int Left {
            get {
                return ((int)(this["Left"]));
            }
            set {
                this["Left"] = value;
            }
        }

        
        
        
        public string Text {
            get {
                return ((string)(this["Text"]));
            }
        }

        
        
        
        public int Top {
            get {
                return ((int)(this["Top"]));
            }
            set {
                this["Top"] = value;
            }
        }

        
        
        
        public int Width {
            get {
                return ((int)(this["Width"]));
            }
            set {
                this["Width"] = value;
            }
        }
    }
}

Here is the full code

```csharp
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>

namespace Examples.Properties {

    
    
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        
        
        
        public global::System.Drawing.Color BackColor {
            get {
                return ((global::System.Drawing.Color)(this["BackColor"]));
            }
            set {
                this["BackColor"] = value;
            }
        }

        
        
        
        public int Height {
            get {
                return ((int)(this["Height"]));
            }
            set {
                this["Height"] = value;
            }
        }

        
        
        
        public int Left {
            get {
                return ((int)(this["Left"]));
            }
            set {
                this["Left"] = value;
            }
        }

        
        
        
        public string Text {
            get {
                return ((string)(this["Text"]));
            }
        }

        
        
        
        public int Top {
            get {
                return ((int)(this["Top"]));
            }
            set {
                this["Top"] = value;
            }
        }

        
        
        
        public int Width {
            get {
                return ((int)(this["Width"]));
            }
            set {
                this["Width"] = value;
            }
        }
    }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

*(a). Settings.cs

Create a file named Settings.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainClass {
    public static void Main() {
      Form form = new Form();
      form.BackColor = Examples.Properties.Settings.Default.BackColor;
      form.Height = Examples.Properties.Settings.Default.Height;
      form.Left = Examples.Properties.Settings.Default.Left;
      form.StartPosition = FormStartPosition.Manual;
      form.Text = Examples.Properties.Settings.Default.Text;
      form.Top = Examples.Properties.Settings.Default.Top;
      form.Width = Examples.Properties.Settings.Default.Width;

      Button buttonColor = new Button();
#if TEST2
      buttonColor.Text = "Test...";
#else
      buttonColor.Text = "Color...";
#endif
      buttonColor.Location = new System.Drawing.Point(10, 10);
      buttonColor.Parent = form;
      buttonColor.Click += delegate (object sender, EventArgs e) {
        ColorDialog colorDialog = new ColorDialog();
        colorDialog.Color = form.BackColor;
        if (colorDialog.ShowDialog() == DialogResult.OK)
          form.BackColor = colorDialog.Color;
      };

      Button buttonSave = new Button();
      buttonSave.Text = "Save";
      buttonSave.Location = new System.Drawing.Point(90, 10);
      buttonSave.Parent = form;
      buttonSave.Click += delegate (object sender, EventArgs e) {
        Examples.Properties.Settings.Default.BackColor = form.BackColor;
        Examples.Properties.Settings.Default.Height = form.Height;
        Examples.Properties.Settings.Default.Left = form.Left;
        Examples.Properties.Settings.Default.Top = form.Top;
        Examples.Properties.Settings.Default.Width = form.Width;
        Examples.Properties.Settings.Default.Save();
      };

      Button buttonReload = new Button();
      buttonReload.Text = "Reload";
      buttonReload.Location = new System.Drawing.Point(170, 10);
      buttonReload.Parent = form;
      buttonReload.Click += delegate (object sender, EventArgs e) {
        Examples.Properties.Settings.Default.Reload();
        form.BackColor = Examples.Properties.Settings.Default.BackColor;
        form.Height = Examples.Properties.Settings.Default.Height;
        form.Left = Examples.Properties.Settings.Default.Left;
        form.Top = Examples.Properties.Settings.Default.Top;
        form.Width = Examples.Properties.Settings.Default.Width;
      };

      Button buttonReset = new Button();
      buttonReset.Text = "Reset";
      buttonReset.Location = new System.Drawing.Point(250, 10);
      buttonReset.Parent = form;
      buttonReset.Click += delegate (object sender, EventArgs e) {
        Examples.Properties.Settings.Default.Reset();
        form.BackColor = Examples.Properties.Settings.Default.BackColor;
        form.Height = Examples.Properties.Settings.Default.Height;
        form.Left = Examples.Properties.Settings.Default.Left;
        form.Top = Examples.Properties.Settings.Default.Top;
        form.Width = Examples.Properties.Settings.Default.Width;
      };
      Application.Run(form);
    }
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Categorized in: