我正确使用.NET中的设置吗?

时间:2010-05-19 03:13:14

标签: c# appsettings

这就是我正在做的事情。

我有三个属性:MomsBackground,DadsBackground和ChosenBackground。

当在程序中选择Momsbackground时,我根据用户点击的项目(“妈妈”或“爸爸”)设置ChosenBackground字符串。

然后在Form_Load()上,我为ChosenBackground字符串使用一个switch case,根据它选择This.BackgroundColor到MomsBackground或DadsBackground。

以下代码:我是否按照预期使用此代码? 抱歉,现在有代码。

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;
            }            
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对我来说听起来不错。如果将来您有更多选择,您可能希望将选项和值存储在数据库中并进行数据库调用以获取适当的值。或者,您可以将键值对存储在app.config中并从那里获取该值。