示例:在应用程序中通过“选项”改变程序中的控制
选择CheckBox,为其Checked属性设置ApplicationSettings。
此操作会
1.自动在Properties中的Settings.settings中添加两个属性值:CloseDocumentPrompt和CloseBatchPrompt。
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CloseDocumentPrompt {
get {
return ((bool)(this["CloseDocumentPrompt"]));
}
set {
this["CloseDocumentPrompt"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CloseBatchPrompt {
get {
return ((bool)(this["CloseBatchPrompt"]));
}
set {
this["CloseBatchPrompt"] = value;
}
}
2.会自动生成App.config文件,其内容为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Sample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Sample.Properties.Settings>
<setting name="CloseDocumentPrompt" serializeAs="String">
<value>True</value>
</setting>
<setting name="CloseBatchPrompt" serializeAs="String">
<value>True</value>
</setting>
</Sample.Properties.Settings>
</userSettings>
</configuration>
**************************************************************************************
Form代码frmOption.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sample.Properties;
namespace Sample{
public partial class frmOption : Form {
private Settings settings;
internal Settings Settings {
get {
return settings;
}
set {
settings = value;
}
}
public frmOption() {
InitializeComponent();
}
private void butnCancel_Click(object sender, EventArgs e) {
this.Close();
}
private void butnApply_Click(object sender, EventArgs e) {
UpdateOptionSetting();
}
private void butnOK_Click(object sender, EventArgs e) {
UpdateOptionSetting();
this.Close();
}
public void UpdateOptionSetting() {
if (chkbCloseDocumentPrompt.Checked)
settings.CloseDocumentPrompt = true;
else
settings.CloseDocumentPrompt = false;
if (chkbCloseBatchPrompt.Checked)
settings.CloseBatchPrompt = true;
else
settings.CloseBatchPrompt = false;
}
private void frmOption_Load(object sender, EventArgs e) {
if (settings.CloseDocumentPrompt)
chkbCloseDocumentPrompt.Checked = true;
else
chkbCloseDocumentPrompt.Checked = false;
if (settings.CloseBatchPrompt)
chkbCloseBatchPrompt.Checked = true;
else
chkbCloseBatchPrompt.Checked = false;
}
private void frmOption_FormClosing(object sender, FormClosingEventArgs e) {
settings.Save();
}
}
}
**************************************************************************************
应用程序中的应用:
private Settings settings = new Settings();
internal Settings Settings {
get {
return settings;
}
}
public void InitializeTreeView() {
if (settings.CloseDocumentPrompt)
ctrvBatch.ShowSaveDocumentPrompt = true; //设置TreeView是否显示保存文档的提示的属性
else
ctrvBatch.ShowSaveDocumentPrompt = false;
settings.SettingChanging +=new System.Configuration.SettingChangingEventHandler(settings_SettingChanging);
}
}
private void settings_SettingChanging(object sender, SettingChangingEventArgs e) {
if (e.SettingName == "CloseDocumentPrompt") {
if ((bool)e.NewValue)
ctrvBatch.ShowSaveDocumentPrompt = true;
else
ctrvBatch.ShowSaveDocumentPrompt = false;
}
}
}
| 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。 |