Am I using Settings in .NET correctly?
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;
}
}
}
}
sounds ok to me. If in the future you have a lot more choices, you may want to store the choices and values in a db and make a db call to get the appropriate value. Alternatively, you could store the key value pairs in the app.config and get that value from there.
精彩评论