1

这就是我正在做的事情。

我有三个属性: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;
            }            
        }
    }
}
4

1 回答 1

0

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

于 2010-05-19T03:16:47.303 回答