如何解决如何使VS 2019程序记住以前的实例首选项?
我目前正在制作文本编辑器,并且创建了一个明暗主题,可以通过按一个按钮进行更改。默认主题为浅色,但我希望它记住我选择的最后一个主题。因此,如果我选择了深色并关闭了它,则下次在启用该主题的情况下打开它。
我还想知道如何使它记住一般的东西。
解决方法
尝试查看Properties.Settings
。
//Sets a string created in the setting
Properties.Settings.Default.FirstString = "Hello World";
//saves the property,this setting will then persist after closing the aplication.
Properties.Settings.Default.Save();
,
我们可以通过Project -> Project Properties… -> Settings
将“主题”保存到“设置”中。
并创建一个名为“主题”的设置,将其值设置为“浅”。
然后我们可以通过以下代码访问“主题”。
private void btnChangeTheme_Click(object sender,EventArgs e)
{
if(Properties.Settings.Default.Theme == "Light")
{
Properties.Settings.Default.Theme = "Dark";
}
else
{
Properties.Settings.Default.Theme = "Light";
}
string theme = Properties.Settings.Default.Theme;
// if(theme == "Light"){...}else{...}
// here is the code that modify theme
// ...
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender,EventArgs e)
{
// load theme
string theme = Properties.Settings.Default.Theme;
// if(theme == "Light"){...}else{...}
// here is the code that modify theme
// ...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。