我是否正确使用了 .NET 中的设置?

发布于 2024-09-01 22:07:41 字数 1782 浏览 4 评论 0原文

这就是我正在做的事情。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-09-08 22:07:41

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文