我是否正确使用了 .NET 中的设置?
这就是我正在做的事情。
我有三个属性: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说听起来不错。如果将来您有更多选择,您可能希望将选择和值存储在数据库中并进行数据库调用以获取适当的值。或者,您可以将键值对存储在 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.