Am I using Settings in .NET correctly?

Posted by Sergio Tapia on Stack Overflow See other posts from Stack Overflow or by Sergio Tapia
Published on 2010-05-19T03:13:14Z Indexed on 2010/05/19 3:30 UTC
Read the original article Hit count: 284

Filed under:
|

Here's what I'm doing.

I have three properties: MomsBackground, DadsBackground and ChosenBackground.

When Momsbackground is selected in the program, I set the ChosenBackground string according to what item the user has clicked (either "Mom" or "Dad").

Then on Form_Load() I use a switch case for the ChosenBackground string and according to that select This.BackgroundColor to MomsBackground or DadsBackground.

Code below: Am I using this as it was intended? Sorry, codes there now.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void momToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.BackColor = Properties.Settings.Default.MomFormColor;
            Properties.Settings.Default.SelectedTheme = "Mom";
            Properties.Settings.Default.Save();
        }

        private void dadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.BackColor = Properties.Settings.Default.DadFormColor;
            Properties.Settings.Default.SelectedTheme = "Dad";
            Properties.Settings.Default.Save();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            switch (Properties.Settings.Default.SelectedTheme)
            {
                case "Mom":
                    this.BackColor = Properties.Settings.Default.MomFormColor;
                    break;
                case "Dad":
                    this.BackColor = Properties.Settings.Default.DadFormColor;
                    break;
                default:
                    break;
            }            
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about appsettings